【问题标题】:How do I keep track of the score and check the answer?我如何跟踪分数并检查答案?
【发布时间】:2013-02-13 08:06:13
【问题描述】:
I trying to make a math quiz game that generates random question and keep track of of what question are wrong and write.

I trying to figure out how to make my program respond if the answer written in the text field is correct or incorrect when I click the JButton `answerbutton`. I'm very new to using `ActionListener`.

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

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

【问题讨论】:

    标签: java


    【解决方案1】:

    如您所说,为了使您的按钮执行某些所需的操作,您需要使用 ActionListener。为此,您需要定义一些实现 ActionListener 接口的类。如果您愿意,该课程可以是您的 PracticeMath 课程。这样做:

    public class PracticeMath extends JFrame implements ActionListener
    {...}
    

    当您这样做时,您的 PracticeMath 类将负责为属于 ActionListener 接口的任何方法指定定义;在这种情况下

    public void actionPerformed( ActionEvent ae )
    {
        // put something intelligent here
    }
    

    然后,您需要将此 ActionListener 添加到您希望它侦听的任何按钮。即

    answerButton.addActionListener( this );
    

    使用“this”作为参数,因为您的 ActionListener 是您的 PracticeMath 类。现在,当您按下应答按钮时,将调用您放入 actionPerformed(...) 方法的任何代码。所以你可以在里面评估你的答案。

    更新以反映以下评论:

    对于 actionPerformed 方法,您的代码应如下所示。

    public void actionPerformed( ActionEvent ae )
    {
        String answer = answerText.getText();
       int answerint = Integer.parseInt(answer);
       if(one + two == answerint){
            correctcounter++;
            System.out.println("correct");
            one = generator.nextInt(11);
            two = generator.nextInt(11);
            // I don't know if you actually store the operator anywhere
            operator = ops[generator.nextInt(4)];
            firstnum.setText("" + one);
               randomOP.setText("" + operator);
               secondnum.setText("" + two);
        }
       else if(one-two == answerint){
           // ... and do the same for the other cases too
    

    当然,这并没有解决您没有检查是否实际使用了正确的运算符这一事实,但这应该可以解决问题,因为第一个答案之后的每个答案都被标记为不正确。

    【讨论】:

    • 好的,所以我让动作监听器开始工作,但是当我输入答案时,它说我的答案是错误的,即使它是正确的。出现的第一个问题很好,但在那之后,它说我所有的答案都是错误的,它仍然不会跟踪我的分数。
    • @DevendraDannyRamdayal 好的,仔细查看您的代码后,我发现了一些可能导致它无法工作的问题。首先,我注意到您使用的所有数字都是整数。如果将一个 int 除以 int,您将得到答案的下限(四舍五入)。让我感到奇怪的另一件事是您检查答案的方式。无论您对两个操作数执行什么操作,它都会接受答案。您不会检查实际操作员应该是什么。奇怪的是,这个错误会导致它说错误的答案是正确的。
    • @DevendraDannyRamdayal 好的...我发现了问题。在第一个问题之后,您未能在 actionPerformed 方法中设置“一”和“二”的值。您设置了 JLabels,但没有设置要比较的值。它们仍然是第一个问题中的样子。所以,如果你每次都给出相同的答案,它应该总是告诉你你是对的。
    • 感谢所有帮助,我真的很感激,但我仍然无法在我的 gui 上跟踪分数
    • @DevendraDannyRamdayal 要跟上分数(数字正确和数字不正确),您只需在 actionPerformed 方法中更新您的 JLabels。你已经在增加你的 correctCounter 和 wrongCounter 变量。现在您只需要更新显示这些值的标签文本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    • 2023-03-18
    • 2018-06-30
    • 1970-01-01
    相关资源
    最近更新 更多