在 ActionListener 中重新定义变量时遇到问题
Having problems with redefining variables within an ActionListener
我有 MathFlash Card 游戏的代码。首先,它会在执行 ActionListener 之前随机化 1 到 12 之间的数字,因为在执行 ActionListener 时,它会检查用户输入的答案是否正确。 ActionListener 中的随机化是为了在检查答案时,无论是正确还是错误,它都会再次随机化问题,以便在再次执行 ActionListener 时,它会检查那些新的随机生成的数字。该代码还让用户知道有多少问题是正确的,有多少问题得到了回答。
我的问题:这里的问题是ActionListener中的随机化需要重新定义变量。如果重新定义变量,则 ActionListener 无法检查在 ActionListener 中生成的那些数字。我尝试将最终修饰符放在每个变量上,但是一旦我这样做,我就会收到错误提示 The "final local variable _______ cannot be assigned, since it is defined in an enclosing type"。感谢任何帮助。
当前代码:
//First Randomization
Random numberOne = new Random();
Random numberTwo = new Random();
final int firstNumberInt = numberOne.nextInt(12) + 1;
final int secondNumberInt = numberTwo.nextInt(12) + 1;
final String firstNumber = Integer.toString(firstNumberInt);
final String secondNumber = Integer.toString(secondNumberInt);
firstNumberPrint.setText(firstNumber);
secondNumberPrint.setText(secondNumber);
//Adding ActionListener
answerBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String answerString = answerField.getText();
//Testing if number can be parsed
try {
int answer = Integer.parseInt(answerString);
//If the answer is correct
if(answer == firstNumberInt + secondNumberInt){
correctEquation.setText("Correct!");
correctEquation.setForeground(Color.GREEN);
//Adding one to equations correct
int equationsCorrectInt = Integer.parseInt(equationsCorrect);
equationsCorrectInt += 1;
String equationsCorrect = Integer.toString(equationsCorrectInt);
equationsCorrectPrint.setText(equationsCorrect + " / 20");
//Adding one to equations answered
int equationsAnsweredInt = Integer.parseInt(equationsAnswered);
equationsAnsweredInt += 1;
String equationsAnswered = Integer.toString(equationsAnsweredInt);
equationsAnsweredPrint.setText(equationsAnswered + " / 20");
//Randomization in the ActionListener
Random numberOne = new Random();
Random numberTwo = new Random();
firstNumberInt = numberOne.nextInt(12) + 1;
secondNumberInt = numberTwo.nextInt(12) + 1;
firstNumber = Integer.toString(firstNumberInt);
secondNumber = Integer.toString(secondNumberInt);
firstNumberPrint.setText(firstNumber);
secondNumberPrint.setText(secondNumber);
}
//If the answer is incorrect
else{
//Setting text to Incorrect
correctEquation.setText("Incorrect!");
correctEquation.setForeground(Color.RED);
//Adding one to equations answered
int equationsAnsweredInt = Integer.parseInt(equationsAnswered);
equationsAnsweredInt += 1;
String equationsAnswered = Integer.toString(equationsAnsweredInt);
equationsAnsweredPrint.setText(equationsAnswered + " / 20");
//Randomization in the ActionListener
Random numberOne = new Random();
Random numberTwo = new Random();
firstNumberInt = numberOne.nextInt(12) + 1;
secondNumberInt = numberTwo.nextInt(12) + 1;
firstNumber = Integer.toString(firstNumberInt);
secondNumber = Integer.toString(secondNumberInt);
firstNumberPrint.setText(firstNumber);
secondNumberPrint.setText(secondNumber);
}
//If number cannot be parsed
} catch(NumberFormatException e) {
error.setText("Number Cannot Be A Word");
}
}
});
您应该将答案检查放在动作侦听器之外的单独方法中,并让动作侦听器使用答案字符串作为参数调用该方法。这样,您可以定义所有变量并在动作侦听器之外使用它们。
示例:
Random rand = new Random();
int randomNumber = rand.nextInt();
button.addActionListener(new ActionListener() {
public void ActionPerformed(ActionEvent e) {
String answer = answerField.getText();
checkAnswer(answer);
}
});
void checkAnswer(String answer) {
if(answer.equals(randomNumber.toString()) {
doSomething();
}
else {
doSomethingElse();
}
}
我有 MathFlash Card 游戏的代码。首先,它会在执行 ActionListener 之前随机化 1 到 12 之间的数字,因为在执行 ActionListener 时,它会检查用户输入的答案是否正确。 ActionListener 中的随机化是为了在检查答案时,无论是正确还是错误,它都会再次随机化问题,以便在再次执行 ActionListener 时,它会检查那些新的随机生成的数字。该代码还让用户知道有多少问题是正确的,有多少问题得到了回答。
我的问题:这里的问题是ActionListener中的随机化需要重新定义变量。如果重新定义变量,则 ActionListener 无法检查在 ActionListener 中生成的那些数字。我尝试将最终修饰符放在每个变量上,但是一旦我这样做,我就会收到错误提示 The "final local variable _______ cannot be assigned, since it is defined in an enclosing type"。感谢任何帮助。
当前代码:
//First Randomization
Random numberOne = new Random();
Random numberTwo = new Random();
final int firstNumberInt = numberOne.nextInt(12) + 1;
final int secondNumberInt = numberTwo.nextInt(12) + 1;
final String firstNumber = Integer.toString(firstNumberInt);
final String secondNumber = Integer.toString(secondNumberInt);
firstNumberPrint.setText(firstNumber);
secondNumberPrint.setText(secondNumber);
//Adding ActionListener
answerBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String answerString = answerField.getText();
//Testing if number can be parsed
try {
int answer = Integer.parseInt(answerString);
//If the answer is correct
if(answer == firstNumberInt + secondNumberInt){
correctEquation.setText("Correct!");
correctEquation.setForeground(Color.GREEN);
//Adding one to equations correct
int equationsCorrectInt = Integer.parseInt(equationsCorrect);
equationsCorrectInt += 1;
String equationsCorrect = Integer.toString(equationsCorrectInt);
equationsCorrectPrint.setText(equationsCorrect + " / 20");
//Adding one to equations answered
int equationsAnsweredInt = Integer.parseInt(equationsAnswered);
equationsAnsweredInt += 1;
String equationsAnswered = Integer.toString(equationsAnsweredInt);
equationsAnsweredPrint.setText(equationsAnswered + " / 20");
//Randomization in the ActionListener
Random numberOne = new Random();
Random numberTwo = new Random();
firstNumberInt = numberOne.nextInt(12) + 1;
secondNumberInt = numberTwo.nextInt(12) + 1;
firstNumber = Integer.toString(firstNumberInt);
secondNumber = Integer.toString(secondNumberInt);
firstNumberPrint.setText(firstNumber);
secondNumberPrint.setText(secondNumber);
}
//If the answer is incorrect
else{
//Setting text to Incorrect
correctEquation.setText("Incorrect!");
correctEquation.setForeground(Color.RED);
//Adding one to equations answered
int equationsAnsweredInt = Integer.parseInt(equationsAnswered);
equationsAnsweredInt += 1;
String equationsAnswered = Integer.toString(equationsAnsweredInt);
equationsAnsweredPrint.setText(equationsAnswered + " / 20");
//Randomization in the ActionListener
Random numberOne = new Random();
Random numberTwo = new Random();
firstNumberInt = numberOne.nextInt(12) + 1;
secondNumberInt = numberTwo.nextInt(12) + 1;
firstNumber = Integer.toString(firstNumberInt);
secondNumber = Integer.toString(secondNumberInt);
firstNumberPrint.setText(firstNumber);
secondNumberPrint.setText(secondNumber);
}
//If number cannot be parsed
} catch(NumberFormatException e) {
error.setText("Number Cannot Be A Word");
}
}
});
您应该将答案检查放在动作侦听器之外的单独方法中,并让动作侦听器使用答案字符串作为参数调用该方法。这样,您可以定义所有变量并在动作侦听器之外使用它们。
示例:
Random rand = new Random();
int randomNumber = rand.nextInt();
button.addActionListener(new ActionListener() {
public void ActionPerformed(ActionEvent e) {
String answer = answerField.getText();
checkAnswer(answer);
}
});
void checkAnswer(String answer) {
if(answer.equals(randomNumber.toString()) {
doSomething();
}
else {
doSomethingElse();
}
}