Reputation: 272
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");
}
}
}
Upvotes: 3
Views: 2330
Reputation: 21459
The problem is that you are resetting x
and y
values after the user enters the answer to the question. That is why the answer is always wrong, hence only total gets incremented.
You should only do:
x = randomGenerator.nextInt(12);
y = randomGenerator.nextInt(12);
when asking the user to provide an answer. Once the user enters an anwser, you perform a check on the provided answer and the current values of x
and y
.
You only regenerate x
and y
on a new quiz session but not during the check.
Upvotes: 2
Reputation: 421340
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++;
Upvotes: 2