2012-03-11 130 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,但不会在检查过程中重新生成。