试图显示一条错误消息,以便当用户输入pin不正确三次时,它将显示错误消息“account blocked”。为什么pinAttempts ++在每次输入不正确的引脚时都不加1?count ++在if语句中不起作用
try {
int pinAttempts = 0;
int pin = Integer.parseInt(enterPinJTextField.getText());
if (pinAttempts == 3) {
JOptionPane.showMessageDialog(popupFrame, "Account blocked!");
}
if (pin != PIN) {
pinAttempts++;
JOptionPane.showMessageDialog(popupFrame, "Please enter correct pin!");
} else {
BankAccount application = new BankAccount();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} catch (NumberFormatException exception) {
JOptionPane.showMessageDialog(popupFrame, "Please enter a number");
}
1 )不要使pinAttempts = 0;如果每次用户按回车时调用它,都将进入try {}。 2)如果(pinAttempts == 3){...把一个返回;返回并且不执行其他代码} – crAlexander 2015-02-11 20:39:14
@Kelv如果其中一个答案适合您,请考虑接受它。 – 2015-02-12 14:02:58