我在增加称为“正确”的计数器时遇到一些困难。而不是增加它不断地打印出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
});
我的猜测是,您期望right
打印出正确猜测的次数,但是看起来好像您打电话
int right = 0;
每次您猜到。
尝试用至少2个相同字母装入一个单词,然后猜测该字母。我敢打赌,在这种情况下,您看到数字上升到2。
如果您希望数字在每次猜对了正确的字母后都增加,那么您将不得不在猜测范围之外进行声明。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句