So I'm trying to have a confirmation dialog box work in my program asking if the user would like to upgrade their membership using import javax.swing.JOptionPane
However, when i go to execute, the program always reverts to as if I clicked "Yes". Help please?
int option =
JOptionPane.showConfirmDialog(null,
"Would you like to become a member for $4.95?",
"Upgrade Membership",
JOptionPane.YES_NO_CANCEL_OPTION);
if (answer == JOptionPane.YES_OPTION)
{
JOptionPane.showMessageDialog(null, "Thank you. Your membership fee will be"
+ " added to your receipt.");
dFee = 4.95;
sMember = "Premium";
}
if (answer == JOptionPane.NO_OPTION)
{
JOptionPane.showMessageDialog(null, "Thank you for your consideration.");
sMember = "Regular";
}
if (answer == JOptionPane.CANCEL_OPTION)
{
JOptionPane.showMessageDialog(null, "You may purchase a membership in the"
+ " future.");
sMember = "Regular";
}
What is perhaps the most apparent problem with the snippet provided is that two different variables are being used for the answer. option
is the variable actually receiving the response from the user, whereas answer
is defined elsewhere, is never assigned to, but is being used in the if
statements.
In short, use option
instead of answer
in the if
statements.