在ActionListener中重新定义变量时遇到问题

时间:2015-08-03 14:16:22

标签: java random actionlistener final

我有一个MathFlash卡游戏的代码。首先,它会在ActionListener执行之前将数字从1到12随机化,因为当执行ActionListener时,它会检查用户输入的答案是否正确。 ActionListener中的随机化是这样的,当检查答案时,无论是正确还是不正确,它都会再次随机化问题,这样当再次执行ActionListener时,它将检查那些新的随机生成的数字。该代码还可以让用户知道有多少问题是正确的,并且有多少问题得到了解答。

我的问题:这里的问题是ActionListener中的随机化要求重新定义变量。如果重新定义了变量,则ActionListener无法检查在ActionListener中生成的那些数字。我尝试将最终修饰符放在每个变量上,但是一旦我这样做,我就会收到错误,说不能分配"最终的局部变量_______,因为它是在封闭类型中定义的#34;。任何帮助表示赞赏。

当前代码:

//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");
                        }
                    }
                });

1 个答案:

答案 0 :(得分:1)

您应该将答案检查放在动作侦听器之外的单独方法中,并让动作侦听器使用答案字符串作为参数调用该方法。这样,您可以定义所有变量并在动作侦听器之外使用它们。

示例:

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();
    }
}