2012-03-11 5 views
3

안녕하세요 여러분, 저에게 도움이 될만한 사람이 있다면 큰 도움이 될 것입니다. 따라서 jtextfield에 값을 입력 할 때이 값이 x * y와 같으면 정확하지 않으면 합계가 같아야 만 합계가 증가합니다. 하지만 지금은 항상 전체를 증가시키고 있습니다. 나는 내가 사용하는 논리가 맞다고 생각하지만 뭔가를 놓치고있다. 이클립스를 사용하고 있으며 프로그램이 컴파일되고 실행 중입니다. 문제는 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

+0

오른쪽. 미안하지만, 나는 그것을 해결하기 위해 무엇을 해야할지 명확하지 않았습니다 ... 당신이 내 대답을 받아 들인 이후에 당신이 그것을 알아 냈다고 가정합니다. 그렇지 않은 경우 알려 주시면 자세히 안내해 드리겠습니다. – aioobe

+0

시간을내어 고쳐 주셨습니다. – Kiril

2

사용자가 질문에 대한 대답을 입력 한 후에 xy 값을 재설정하는 것이 문제입니다. 이것이 답변이 항상 틀린 이유입니다. 따라서 총계 만 증분됩니다. 당신이 오직해야 답을 제공하기 위해 사용자가 요청할 때

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

. 사용자가 answser에 입장하면 제공된 대답과 현재 값이 xy인지 확인합니다. 새 퀴즈 세션에서만 xy을 재생성하지만 체크하지는 않습니다.

관련 문제