I am trying to create a math program for kids for a school project that asks 10 questions. I am having trouble finding out how and where to apply a loop in my code. I have tried to use for loop inside my enterbtnListener and startBtnListener but unfortunately none have work. This is the output of the program. program_OUTPUT
Here is the code of the startBtnListener
class startBtnListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (add.isSelected()) {
ansBtn.setEnabled(true);
input.setEditable(true);
result = num1 + num2;
question = ("What is " + num1 + " + " + num2 + " = ");
questionTOP.setVisible(false);
questionHERE.setText(question);
questionHERE.setVisible(true);
input.setVisible(true);
add.setEnabled(false); //disables buttons until END button is clicked
subtract.setEnabled(false);
multiply.setEnabled(false);
beginner.setEnabled(false);
intermediate.setEnabled(false);
advanced.setEnabled(false);
} else if (subtract.isSelected()) {
ansBtn.setEnabled(true);
input.setEditable(true);
result = num1 - num2;
question = ("What is " + num1 + " - " + num2 + " = ");
questionTOP.setVisible(false);
questionHERE.setText(question);
questionHERE.setVisible(true);
input.setVisible(true);
add.setEnabled(false); //disables buttons until END button is clicked
subtract.setEnabled(false);
multiply.setEnabled(false);
beginner.setEnabled(false);
intermediate.setEnabled(false);
advanced.setEnabled(false);
} else if (multiply.isSelected()) {
ansBtn.setEnabled(true);
input.setEditable(true);
result = num1 * num2;
question = ("What is " + num1 + " * " + num2 + " = ");
questionTOP.setVisible(false);
questionHERE.setText(question);
questionHERE.setVisible(true);
input.setVisible(true);
add.setEnabled(false); //disables buttons until END button is clicked
subtract.setEnabled(false);
multiply.setEnabled(false);
beginner.setEnabled(false);
intermediate.setEnabled(false);
advanced.setEnabled(false);
} else {
ansBtn.setEnabled(false);
input.setEditable(false);
JOptionPane.showMessageDialog(null, "You need to choose below!", "PLEASE NOTE", JOptionPane.WARNING_MESSAGE);
}
}
}
Here is the code of the enterBtnListener
class enterListener implements KeyListener {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
if (beginner.isSelected()) {
num1 = (int) (Math.random() * 11);
num2 = (int) (Math.random() * 11);
} else if (intermediate.isSelected()) {
num1 = (int) (Math.random() * 9 + 11);
num2 = (int) (Math.random() * 9 + 11);
} else if (advanced.isSelected()) {
num1 = (int) (Math.random() * 17 + 13);
num2 = (int) (Math.random() * 17 + 13);
}
// checking answer is correct or wrong
while (true) {
double doubleOfInput = Double.parseDouble(input.getText()); // getting string to double
if (doubleOfInput == result) {
JOptionPane.showMessageDialog(null, "correct");
input.setText(" "); // clearing input after user entered answer
break;
} else {
JOptionPane.showMessageDialog(null, "Wrong, Try Again!");
input.setText(" ");
}
}
//generating formula
if (add.isSelected()) {
result = num1 + num2;
question = ("What is " + num1 + " + " + num2 + " = ");
} else if (subtract.isSelected()) {
result = num1 - num2;
question = ("What is " + num1 + " - " + num2 + " = ");
} else if (multiply.isSelected()) {
result = num1 * num2;
question = ("What is " + num1 + " * " + num2 + " = ");
} else {
result = num1 + num2;
question = ("What is " + num1 + " + " + num2 + " = ");
}
questionHERE.setText(question);//setting new question
}
}
I would very much appreciate if someone can help me. Thanks.
Looks good! My opinion would be to have a design where
A start button
-> listener will initiate this math quiz.
A next button
-> listener will have a iterate a class (instance level) variable and you can have it until it reaches your desired count (10 say)
An End button
-> listener will terminate this math quiz and possibly should post the results. Possibly have this terminate and show result logic in a separate method and call here. (method name: terminateQuiz (say))
The next button whose listener will look roughly like,
class nextBtnListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//count++;
//when count reaches 10 (say) it should call terminateQuiz
}
}
All other things looks fine..
Note: Reason for Next button gives better look and feel than an enter key.
//Happy learning!
I am assuming your enterListener class is one which is handling ANSWER button for gui and flow is like below:
StartButton - will initialize the quiz and show the initial question Answer Button - will accept the answer entered by user and will show next button.
So your loop may go like below in enterListener
class enterListener implements KeyListener {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
if (beginner.isSelected()) {
num1 = (int) (Math.random() * 11);
num2 = (int) (Math.random() * 11);
} else if (intermediate.isSelected()) {
num1 = (int) (Math.random() * 9 + 11);
num2 = (int) (Math.random() * 9 + 11);
} else if (advanced.isSelected()) {
num1 = (int) (Math.random() * 17 + 13);
num2 = (int) (Math.random() * 17 + 13);
}
for(int i = 1 ; i < 10 ;i++){ //start your loop with 1 till 9 showing 9 questions as first one is already start with startbutton
// checking answer is correct or wrong
while (true) {
double doubleOfInput = Double.parseDouble(input.getText()); // getting string to double
if (doubleOfInput == result) {
JOptionPane.showMessageDialog(null, "correct");
input.setText(" "); // clearing input after user entered answer
break;
} else {
JOptionPane.showMessageDialog(null, "Wrong, Try Again!");
input.setText(" ");
}
}
//generating formula
if (add.isSelected()) {
result = num1 + num2;
question = ("What is " + num1 + " + " + num2 + " = ");
} else if (subtract.isSelected()) {
result = num1 - num2;
question = ("What is " + num1 + " - " + num2 + " = ");
} else if (multiply.isSelected()) {
result = num1 * num2;
question = ("What is " + num1 + " * " + num2 + " = ");
} else {
result = num1 + num2;
question = ("What is " + num1 + " + " + num2 + " = ");
}
questionHERE.setText(question);//setting new question
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.