試圖顯示一條錯誤消息,以便當用戶輸入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