我在增加我的计数器方面遇到了一些困难。不是递增它而是不断地打印1并且不将数字加在一起。我尝试使用另一个变量来累计计数器,但没有运气。任何想法为什么它没有递增?
代码:
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
});
答案 0 :(得分:5)
我的猜测是,您希望right
打印您猜对了的次数,但看起来好像在打电话
int right = 0;
每次猜到。
尝试加载至少有2个同一个字母的单词,并猜测该字母。我敢打赌,在这种情况下你会看到这个数字达到2。
如果你想在每次猜到一个正确的字母时增加一个数字,那么你将不得不在猜测范围之外宣布它。