使此确认对话框在 java 中有效

Making this confirmation dialog work in java

所以我试图在我的程序中使用一个确认对话框,询问用户是否愿意使用 import javax.swing.JOptionPane

升级他们的会员资格

然而,当我去执行时,程序总是恢复到我点击 "Yes" 时的状态。请帮忙?

int option =
                     JOptionPane.showConfirmDialog(null,
                             "Would you like to become a member for .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";
             }

所提供的代码片段最明显的问题可能是使用了两个不同的变量作为答案。 option 是实际接收用户响应的变量,而 answer 在别处定义,从未分配给,但在 if 语句中使用。

简而言之,在 if 语句中使用 option 而不是 answer