2012-03-11 12 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入力された値

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

サイドノート:

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

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


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

新しく生成された質問に対して、すでに入力された値の結果を確認します

と表すことができる
+0

申し訳ありませんが、サイドノートとは別に、あなたが書いたコードと私のものの違いが分かりません – Kiril

+0

申し訳ありませんが、私はそれを修正するために何をすべきかについて明確ではありませんでした...私はあなたが私の答えを受け入れてからそれを理解したと仮定します。そうでない場合は、私に知らせてください、私はあなたをさらに案内します。 – aioobe

+0

私はそれを修正しました、あなたの時間のおかげで! – Kiril

2

問題は、ユーザーが質問に対する回答を入力した後にxyの値をリセットしてしまうことです。答えが常に間違っているので、合計が増分されるのはそのためです。 あなただけ行う必要があります。

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

答えを提供するために、ユーザに尋ねるとき。ユーザーがanswserに入ると、提供された回答と現在の値であるxyのチェックを実行します。 新しいクイズセッションではxyのみを再生成しますが、チェック中は再生しません。