I'm having some difficulty incrementing my counter called right. Instead of incrementing it constantly prints out 1 and doesn't add the numbers together. I tried using another variable to total up the counter but no luck. Any ideas to why it is not incrementing?
Code:
tf.addActionListener(new ActionListener() {
int wrong = 0;
int right = 0;
@Override
public void actionPerformed(ActionEvent e) {// enter key
JTextField tf = (JTextField) e.getSource();
letter = tf.getText();
tf.setText("");
// tf.requestFocus();
jlLetsUsed.setText(jlLetsUsed.getText() + letter + " ");// sets
char[] jlabelText = jlLines.getText().toCharArray();// converts
char userEnteredChar = letter.charAt(0);
System.out.println(wordList[level]);
if (!wordList[level].contains(letter)) {
wrong++;
if (wrong == 6) {
JOptionPane.showMessageDialog(frame,
"He's dead, game over." + "\n"
+ "The word was " + wordList[level]
+ ".", "You Lost",
JOptionPane.INFORMATION_MESSAGE, ic);
GameStructure restart = new GameStructure();
level = (int) (Math.random() * 64);// generate new
// random word
restart.window();
}
return;
}
int i = 0;
for (i = 0; i < wordList[level].length(); i++) {
if (wordList[level].charAt(i) == userEnteredChar) {
jlabelText[3 * i] = ' ';
jlabelText[3 * i + 1] = userEnteredChar;
right++;
}// end if
}// end for
jlLines.setText(String.valueOf(jlabelText));
if (jlabelText.length / 3 == right) {
JOptionPane.showMessageDialog(frame, "You got the word!.",
"You Lost", JOptionPane.INFORMATION_MESSAGE, ic);
GameStructure restart = new GameStructure();
level = (int) (Math.random() * 64);// generate new
// random word
restart.window();
}
}// end actionPerformed method
});
My guess is that you're expecting right
to print the number of times you've guessed right, but it appears as though you call
int right = 0;
every time you guess.
Try loading up a word with at least 2 of the same letter, and guess that letter. I'll bet that you see the number go up to 2 in that case.
If you want a number to increment every time you've guessed a correct letter, than you're going to have to declare it outside the scope of your guess.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.