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

标签 java random actionlistener final

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

我的问题:这里的问题是 ActionListener 中的随机化需要重新定义变量。如果重新定义变量,则 ActionListener 无法检查在 ActionListener 中生成的那些数字。我尝试在每个变量上添加最终修饰符,但是一旦这样做,我就会收到错误消息“无法分配最终局部变量_______,因为它是在封闭类型中定义的”。如有任何帮助,我们将不胜感激。

当前代码:

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

关于java - 在 ActionListener 中重新定义变量时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31789646/

相关文章:

java - adaptivethreshold 给出错误(opencv,java)

java - URL 无法打开文件 :\C filepath in Java

sql - postgresql 中带有计数的分层随机样本

java - 我是否重载了我的actionListener?文件选择器

java - 编程时属性不会出现在 Eclipse 中

java - 尝试用数组获取不重复的随机数

Python — 用设定数量的 1 随机填充二维数组

java - 如何自定义ActionEvent的事件源?

java - 如何将 JTextField 中的数据显示到 JTextArea 中?

java - 为什么 DialogFragment.dismiss 不立即终止对话框?