I want to display 5 different comments when a button (Answer Btn) is clicked.
Here we can see the JOption message dialog showing "Correct" comment.
When I click the Answer button again, I want it to display another comment like "Great Job".
Here is the code for my ansBtnListener
class ansBtnListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// checking answer is correct or wrong
double doubleOfInput = Double.parseDouble(input.getText()); // getting string to double
if (doubleOfInput == result) {
JOptionPane.showMessageDialog(null, "Correct");
} else {
JOptionPane.showMessageDialog(null, "Wrong, Try Again!");
input.setText(" ");
}
}
}
Any help is much appreciated.
You can add a method local variable and initially assign that to zero. So for the first time if the user is answering also check the value of that variable in if clause. If the value is 0 then show 'Correct' and increment the value by 1, and if the user is pressing the answer button again you will get that from the value of the variable and show 'Great Job' or 'Excellent' as required then.
class ansBtnListener implements ActionListener {
int count = 0;
public void actionPerformed(ActionEvent e) {
// checking answer is correct or wrong
double doubleOfInput = Double.parseDouble(input.getText()); // getting string to double
if (doubleOfInput == result && count==0) {
JOptionPane.showMessageDialog(null, "Correct");
count++;
}
else if (doubleOfInput == result && count==1) {
JOptionPane.showMessageDialog(null, "Great Job!");
count++;
}
else if (doubleOfInput == result && count==2) {
JOptionPane.showMessageDialog(null, "Excellent!");
count++;
}
else if (doubleOfInput == result && count>2) {
JOptionPane.showMessageDialog(null, "Please click on end!");
}
else {
JOptionPane.showMessageDialog(null, "Wrong, Try Again!");
input.setText(" ");
}
}
}