0

我试图制作一个数学问答游戏,它会生成随机问题并跟踪哪些问题是错误的并写出来。

让动作监听器工作,但是当我输入答案时,它说我的答案是错误的,即使它是正确的。出现的第一个问题很好,但在那之后,它说我所有的答案都是错误的,即使它们是正确的并跟踪分数。

 import java.util.Scanner;
        import javax.swing.*;
        import javax.swing.border.TitledBorder;

        import java.util.Random;
        import java.util.Scanner;
        import java.awt.*;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;

        public class PracticeMath extends JFrame implements ActionListener {
            Scanner k = new Scanner(System.in);
            Random generator = new Random();
            protected JButton excerciseButton = new JButton( "New Excerices" ); // start new quiz session
            protected JButton answerButton = new JButton( "Answer" ); // set new question, check if the answer correct or wrong
            protected JLabel titlelabel = new JLabel( "How much is: " ); 
            protected int correctcounter = 0; // keep track of correct answer
            protected int wrongcounter = 0; // keep track of wrong answer
            protected int one = generator.nextInt(10);//generate ranodm first number of question
            protected int two = generator.nextInt(10); // generate random second number of question
            protected int i = generator.nextInt(4); // generate random operator
            protected char[] ops = { '+', '-', '/', '*' }; // the math operator
            protected JLabel correctlabel = new JLabel(" Number of Correct Answer: ");
            protected JLabel wronglabel = new JLabel( " Number of  Wrong answers:  " );
            protected JLabel firstnum = new JLabel("" + one); // display first number
            protected JLabel secondnum = new JLabel("" + two); //  display second number
            protected JLabel randomOP = new JLabel("" + ops[i]); // display operator
            protected JLabel equalOP = new JLabel("=");
            protected JTextField answerText = new JTextField(); //text area for writing you answer
            protected JLabel questionmark = new JLabel("?");
            protected JLabel correct = new JLabel(""+ correctcounter); // display correct answer
            protected JLabel wrong = new JLabel(""+ wrongcounter); // display wrong answer
            protected JLabel commentlabel = new JLabel(""); // set a comment for how good you doing. optionial




            public PracticeMath(){

                answerText.setColumns(5);

                JPanel Panel1 = new JPanel();// add a panel
                FlowLayout flowLayout = (FlowLayout) Panel1.getLayout();// layout for panel
                getContentPane().setLayout( new FlowLayout( FlowLayout.LEFT, 5, 5)); // set layout
                getContentPane().add(Panel1); // set panel
                titlelabel.setForeground(Color.ORANGE); 
                titlelabel.setFont(new Font("Tahoma", Font.PLAIN, 25));

                Panel1.add(titlelabel);
                firstnum.setFont(new Font("Tahoma", Font.PLAIN, 20));
                Panel1.add(firstnum);

                randomOP.setFont(new Font("Tahoma", Font.PLAIN, 25));
                Panel1.add(randomOP);

                secondnum.setFont(new Font("Tahoma", Font.PLAIN, 20));

                Panel1.add(secondnum);

                equalOP.setFont(new Font("Tahoma", Font.PLAIN, 20));
                Panel1.add(equalOP);

                Panel1.add(answerText);

                questionmark.setFont(new Font("Tahoma", Font.PLAIN, 20));
                Panel1.add(questionmark);

                Panel1.add(commentlabel);



                JPanel Panel3 = new JPanel();
                FlowLayout flowLayout3 = (FlowLayout) Panel3.getLayout();
                flowLayout3.setHgap(15);
                getContentPane().setLayout( new FlowLayout( FlowLayout.LEFT, 5, 5));
                getContentPane().add(Panel3);
                Panel3.add(excerciseButton);
                Panel3.add(answerButton);



                    JPanel panel2 = new JPanel();
                panel2.setBorder(new TitledBorder("Statistic"));
                getContentPane().add(panel2);
                panel2.setLayout(new GridLayout(0, 2, 0, 0));
                panel2.add(correctlabel);
                panel2.add(wronglabel);
                correct.setForeground(Color.GREEN);
                correct.setFont(new Font("Tahoma", Font.PLAIN, 25));
                panel2.add(correct);

                wrong.setForeground(Color.RED);
                wrong.setFont(new Font("Tahoma", Font.PLAIN, 25));
                panel2.add(wrong);




            answerButton.addActionListener( this );

            }






           public static void main(String[] args) {

                PracticeMath frame = new PracticeMath();
                frame.pack();
                frame.setLocationRelativeTo(null);  
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(400,400);
                frame.setTitle( "Math Practice");
                frame.setVisible(true);





            }

         public void actionPerformed( ActionEvent ae )
   {
       String answer = answerText.getText();
       int answerint = Integer.parseInt(answer);
       if(one + two == answerint){
            correctcounter++;
            System.out.println("correct");
            firstnum.setText("" + generator.nextInt(11));
               randomOP.setText("" + ops[generator.nextInt(4)]);
               secondnum.setText("" + generator.nextInt(11));


        }
       else if(one-two == answerint){
            correctcounter++;
            System.out.println("correct");
            firstnum.setText("" + generator.nextInt(11));
               randomOP.setText("" + ops[generator.nextInt(4)]);
               secondnum.setText("" + generator.nextInt(11));
        }
        else if(one * two ==answerint){
            correctcounter++;
            System.out.println("correct");
            firstnum.setText("" + generator.nextInt(11));
               randomOP.setText("" + ops[generator.nextInt(4)]);
               secondnum.setText("" + generator.nextInt(11));
        }else if(one/two == answerint){
            correctcounter++;
            System.out.println("correct");
            firstnum.setText("" + generator.nextInt(11));
               randomOP.setText("" + ops[generator.nextInt(4)]);
               secondnum.setText("" + generator.nextInt(11));
        }
        else{
            wrongcounter++;
            System.out.println("wrong");
            firstnum.setText("" + generator.nextInt(11));
               randomOP.setText("" + ops[generator.nextInt(4)]);
               secondnum.setText("" + generator.nextInt(11));
        }
   }
}
4

2 回答 2

1

您永远不会重新分配变量oneand two,因此它们仍然使用与它们开始时相同的值...

尝试重新构建您的actionPerformed方法以减少代码重复并将新方程值分配给onetwo变量....

public void actionPerformed(ActionEvent ae) {
    String answer = answerText.getText();
    int answerint = Integer.parseInt(answer);
    if (one + two == answerint) {
        correctcounter++;
        System.out.println("correct");
    } else if (one - two == answerint) {
        correctcounter++;
        System.out.println("correct");
    } else if (one * two == answerint) {
        correctcounter++;
        System.out.println("correct");
    } else if (one / two == answerint) {
        correctcounter++;
        System.out.println("correct");
    } else {
        wrongcounter++;
        System.out.println("wrong");
    }
    one = generator.nextInt(11);
    two = generator.nextInt(11);
    firstnum.setText("" + one);
    randomOP.setText("" + ops[generator.nextInt(4)]);
    secondnum.setText("" + two);
}

更新

当您维护正确和错误答案的计数器时,您没有在任何地方显示它们(第一次运行程序时吓坏了我)。

actionPerformed方法的底部,您需要更新显示正确和错误答案数量的标签。

public void actionPerformed(ActionEvent ae) {
    // ... Previous content... //

    correct.setText(Integer.toString(correctcounter));
    wrong.setText(Integer.toString(wrongcounter));
}
于 2013-02-12T23:58:24.573 回答
1

actionPerformed中,您似乎在回答每个问题后使用新的随机值更新文本字段 (firstnumsecondnum),但您正在使用变量one和检查答案two。由于显示的随机值onetwo没有更新,它们与您回答第一个问题后显示的内容不同步。

于 2013-02-12T23:50:52.747 回答