2012-03-11 98 views
3

大家好我是堆棧,所以如果任何人都可以給我一個幫助,那就太好了。因此,當我在jtextfield中輸入一些值時,如果這個值與x * y中的值相同,則它應該正確執行,並且如果它們不相同,它們應該增加總數。但目前它總是增加總量。我認爲我使用的邏輯是正確的,但我錯過了一些東西。我正在使用eclipse,程序正在編譯和運行。 我想問題是在actionPerformed方法的PanelQuizCountdown類中。這是代碼。將價值傳遞給JLabel

/**The driver class of the program. Here is the JFrame 
* class name RunQuizCountdown.java 
* @author Kiril Anastasov 
* @date 09/03/2012 
*/ 

import java.awt.*; 
import javax.swing.*; 

public class RunQuizCountdown 
{ 
    public static void main(String[] args) 
    { 
     JFrame application = new JFrame(); 
     PanelQuizCountdown panel = new PanelQuizCountdown(); 
     application.add(panel); 
     application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     application.setSize(200,300); 
     application.setLocationByPlatform(true); 
     application.setVisible(true); 
    } 

} 


/** Here is the thread of the program 
* class name ThreadQuizCountdown.java 
* @author Kiril Anastasov 
* @date 09/03/2012 
*/ 

import javax.swing.JTextField; 

public class ThreadQuizCountdown implements Runnable 
{ 
    JTextField timeField; 
    Thread myThread = new Thread(this); 
    int i = 30; 
    boolean go = true; 

    ThreadQuizCountdown(JTextField theTimeField) 
    { 
     timeField = theTimeField; 
    } 

    public void run() 
    {  
     while(go) 
     {       
      timeField.setText("" + i);  
      try 
      { 
       myThread.sleep(1000);   
      } 
      catch (InterruptedException ie) 
      { 
       System.out.println("thread exception"); 
      }  
      if(i == 0) 
      { 
       //go = false; 
       myThread.stop(); 
      } 
      i--; 
     }  
    } 

    public void begin() 
    { 
     myThread.start(); 
    } 

    public void finish() 
    { 
     myThread.stop(); 
    } 
} 
/** Here is the GUI of the program 
* class name PanelQuizCountdown.java 
* @author Kiril Anastasov 
* @date 09/03/2012 
*/ 

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 
import java.util.Random; 

public class PanelQuizCountdown extends JPanel implements ActionListener 
{ 
    JTextField timeField, answerField; 
    JLabel messageLabel, correctLabel, totalLabel; 
    int x, y; 
    int correct; 
    int total; 
    int result; 
    int check; 
    Random randomGenerator; 

    ThreadQuizCountdown myQuiz; 

    PanelQuizCountdown() 
    { 
     timeField = new JTextField(5); 
     myQuiz = new ThreadQuizCountdown(timeField); 
     this.add(timeField); 
     myQuiz.begin(); 

     randomGenerator = new Random(); 
     x = randomGenerator.nextInt(12); 
     y = randomGenerator.nextInt(12);   

     messageLabel = new JLabel("What is the result of " + x + " * " + y); 
     this.add(messageLabel); 

     answerField = new JTextField(5); 
     answerField.addActionListener(this); 
     this.add(answerField); 

     correctLabel = new JLabel("You gave : " + correct + " correct answers"); 
     this.add(correctLabel); 

     totalLabel = new JLabel("Of total: " + total + " questions"); 
     this.add(totalLabel);  
    } 

    public void actionPerformed(ActionEvent ae) 
    { 
     if(ae.getSource() == answerField) 
     { 
      randomGenerator = new Random(); 
      x = randomGenerator.nextInt(12); 
      y = randomGenerator.nextInt(12); 
      messageLabel.setText("What is the result of " + x + " * " + y); 
      System.out.println("Expected: " + result); 
      result = x * y; 
      String s = answerField.getText(); 
      answerField.setText(""); 
      check = Integer.parseInt(s); 


      System.out.println("Your answer: " + check); 

      if(result == check) 
      { 
       correct++; 
       total++; 
      } 
      else 
      { 
       total++; 
      } 

      correctLabel.setText("You gave : " + correct + " correct answers"); 
      totalLabel.setText("Of total: " + total + " questions"); 


     } 

    } 
} 

回答

2

但是要更新預期的結果你輸入的結果正確之前:

生成新的隨機因素:

 randomGenerator = new Random(); 
     x = randomGenerator.nextInt(12); 
     y = randomGenerator.nextInt(12); 

變化問題,併產生新的result

 messageLabel.setText("What is the result of " + x + " * " + y); 
     System.out.println("Expected: " + result); 
     result = x * y; 

獲取當前文本LY輸入的值

 String s = answerField.getText(); 
     answerField.setText(""); 
     check = Integer.parseInt(s); 


     System.out.println("Your answer: " + check); 

檢查對新產生的問題已經進入價值的結果:

 if(result == check) 
     { 
      correct++; 
      total++; 
     } 

一個側面說明:

if(result == check) 
{ 
    correct++; 
    total++; 
} 
else 
{ 
    total++; 
} 

可以表示爲

total++; 
if (result == check) 
    correct++; 
+0

對不起,但除了旁註,我沒有看到你寫的代碼和我的一個 – Kiril 2012-03-11 12:40:18

+0

正確的區別。對不起,我不清楚要做什麼來糾正它......我想你自從你接受了我的答案後就明白了。如果沒有,讓我知道,我會進一步引導你。 – aioobe 2012-03-11 15:37:51

+0

我改正了,感謝您的時間! – Kiril 2012-03-11 16:23:48

2

問題是在用戶輸入問題的答案後,您將重置xy值。這就是爲什麼答案總是錯誤的,因此只有總數增加。 你應該只做:

x = randomGenerator.nextInt(12); 
y = randomGenerator.nextInt(12); 

當要求用戶提供答案。一旦用戶輸入安裝程序,您將對提供的答案和當前值xy執行檢查。 您只能在新的測驗會話中重新生成xy,但不會在檢查過程中重新生成。