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.