Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7614061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:18:25+00:00 2026-05-31T02:18:25+00:00

Hi everyone I am stack so if anyone can give me a help it

  • 0

Hi everyone I am stack so if anyone can give me a help it would be great. So when I enter some value in the jtextfield and if this value is the same as the one of x * y it should inrcement correct and total if they are not the same it should increment only total. but at the moment is it always incrementing the total. I think the logic i am using is correct but i am missing something. I am using eclipse and the program is compiling and running.
I guess the issues is 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");


        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:18:27+00:00Added an answer on May 31, 2026 at 2:18 am

    But you are updating the expected result right before you get the entered result:

    Generate new random factors:

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

    Change the question and generate new 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 against the newly generated question:

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

    A side-note:

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

    can be expressed as

    total++;
    if (result == check)
        correct++;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like some opinion, example and code on how everyone dose this.. So
Hi everyone I am stack so if anyone can assist in any way it
How can i realize an UiTableWiev like this? https://i.stack.imgur.com/8QVb4.jpg I appreciate everyone who gives
So basically everyone at one point needs to do this,load some images from web
Everyone uses source-code control to manage versions (right?) and this provides some level of
Greetings everyone: I would love to get some information from a huge collection of
Hello everyone this is my first post on stack overflow.com I am trying to
I've seen this all over the place on stack overflow, but everyone else's solution
this is my first question on stack overflow but I have some experience in
Thank everyone for the help I have made some really good changes but now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.