By passing the JLabel value

Hi everyone, I am standing, so if anyone can help me that would be great. So when I enter some value in the jtextfield and if that value is the same as x * y, it should be correct and summarized, if they do not match, it should only increment the total. but at the moment it always increases the total. I think the logic I am using is correct, but I am missing something. I am using eclipse and the program compiles and works. I think the problems are in the PanelQuizCountdown class in the actionPerformed method. Here is the code.

/**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");


        }

    }
}

      

+3


source to share


2 answers


But you are updating the expected result before getting the injected result:

Create new random factors:

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

      

Modify the question and generate a new one result

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

      

Get the text of the currently entered value :

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


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

      



Check the result of the already entered value in the newly created question :

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

      


Note:

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

      

can be expressed as

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

      

+2


source


The problem is that you are returning values x

and y

after the user enters the answer to the question. This is why the answer is always wrong, so only the total number increases. You should only do:

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

      



when the user asks for a response. When the user enters anwser, you validate the provided response and the current values x

and y

. You are recovering x

and y

the new session of the quiz, but not during the test.

+2


source







All Articles