2013-02-12 24 views
0
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)); 
     } 
    } 
} 

回答

0

爲了讓你的按鈕做一些你想要的動作,就像你說的那樣,你需要使用一個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 

這着,當然,沒有解決的事實,你不檢查,以查看是否實際使用了正確的操作,但這應該與每一個答案,過去的第一個被標記爲不正確解決問題。

+0

好吧,所以我得到了行動聽衆的工作,但是當我輸入答案時,它說我的答案是錯誤的,即使它是正確的。第一個問題看起來工作正常,但在此之後,它說我的答案都是錯誤的,它仍然不會記錄我的分數。 – 2013-02-12 23:08:58

+0

@DevendraDannyRamdayal好吧,仔細看看你的代碼,我發現了一些可能導致它無法工作的問題。首先,我注意到你使用的所有數字都是整數。如果用int和int分隔int,則會得到答案的下限(舍入)。另一件讓我感到奇怪的事情是你檢查答案的方式。無論您在兩個操作數上執行什麼操作,它都會接受答案。你不檢查真正的運營商應該是什麼。奇怪的是,這個錯誤會導致它說錯誤的答案是正確的。 – Nathan 2013-02-12 23:54:03

+0

@DevendraDannyRamdayal好吧......我發現了這個問題。第一個問題後,您無法在actionPerformed方法中設置「one」和「two」的值。您設置JLabels,但不設置您正在比較的值。他們仍然是第一個問題。所以,如果你每次都回答相同的答案,它總是會告訴你,你是正確的。 – Nathan 2013-02-13 00:00:07