'How do I use the same variable between buttons?
I've created a rock paper scissors game in java, but I don't know how I could use the same variable to work between 3 different buttons (rock, paper scissors). I use if loops for win loss and tie in each button so I could easily just add 1 to a variable but I'm not sure how to make it work everywhere in my code and if that is something even possible.
if (playerChoice.equals(randomChoice)) //If player chose the same thing as the computer
{
//TIE
System.out.println("user tied"); //Tie the game with output message in console
lblGameStatus.setText("The game tied!"); //Output to the user for the result of the game
}
if (randomChoice.equals("paper")) //and if the computer chose paper
{
//ROCK VS PAPER
System.out.println("user won");//The player wins, with output message in console
lblGameStatus.setText("You Won!"); //Output to the user for the result of the game
}
else if (randomChoice.equals("scissors")) //if the computer chose scissors instead of paper
{
//ROCK VS SCISSORS
System.out.println("user lost"); //the computer wins, display output message in console
lblGameStatus.setText("You Lost!"); //Output to the user for the result of the game
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|