lundi 3 août 2015

Having problems with redefining variables within an ActionListener

I have this code for a MathFlash Card game. First, it will randomize the numbers from 1 to 12 before the ActionListener is executed since, when the ActionListener is executed, it checks if the answer the user inputted was correct. The randomization in the ActionListener is so that when the answer is checked, either correct or incorrect, it will randomize the questions again so that when the ActionListener is executed again, it will check for those new randomly generated numbers. The code also lets the user know how many questions were correct and how many questions were answered.

My Problem: The problem here is that the randomization in the ActionListener requires that the variables be redefined. If the variables are redefined, the ActionListener cannot check for those numbers that were generated in the ActionListener. I've try to put the final modifier on each variable, but once I do that, I get errors saying The "final local variable _______ cannot be assigned, since it is defined in an enclosing type". Any help is appreciated.

The current code:

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




Aucun commentaire:

Enregistrer un commentaire