Issue
I have a simple hangman game that has a Jlabel that is supposed to show how many times the word was guessed right. It uses a simple counter wins++ and will show up properly after the first win but any after won't work. The counter won't add more so do I need to use a loop in some way?
if (word.equals(dashes.toString()))
{
wordOutput.setText("You Win!");
wins++; //add 1 to win counter
winsOutput.setText("Wins: " + wins);
}
Seems like this should be simple but I don't know what's wrong
Solution
I think you made the win variable as a local, which will get its initial stage after every execution, make the win variable outside of the method and you can also make it static.
public class Hangman {
// here is the variable..
private int wins = 0;
// Here are your other methods..
public void processGame(){
}
}
By the way, it will also works fine without static if your game does not have more than one classes etc. Give it a try with static and without it, but the main point is initializing it outside of the class.
Answered By - Rehan Javed