Java 计数++;在if语句中不工作

Java 计数++;在if语句中不工作,java,Java,尝试显示错误消息,以便当用户输入错误的pin码三次时,其将显示错误消息“account blocked”。为什么每次输入不正确的pin时,pin+不添加1 try { int pinAttempts = 0; int pin = Integer.parseInt(enterPinJTextField.getText()); if (pinAttempts == 3) { JOptionPane.showMessageDialog(popupFrame, "

尝试显示错误消息,以便当用户输入错误的pin码三次时,其将显示错误消息“account blocked”。为什么每次输入不正确的pin时,pin+不添加1

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");
}

因为您再次将其设置为
0

int pinAttempts = 0;

每次输入try语句时,都将PINTERPENTS设置为0。这意味着以前的一切都将被改写

您应该将
pintattests=0
移动到发生初始化的某个位置,以便只发生一次。或者将其放在一种可以为用户重置尝试计数器的方法中

为什么每次输入不正确的pin时,pin++都不添加1 进入

别担心,是你的理解错了

您需要意识到,每次使用重新输入
try
块时,您都在将
pintruments
重新初始化为0

int=0在开头


全局声明变量(在类的顶部),而不是在
try/catch
块中声明它,它将起作用。

问题是
pintruts
一次又一次地使用值
0

将您的
pintruments
设置为静态,或者将其声明到某个无法反复初始化的位置,我将这样做

static int pinAttempts=0;
public boolean isLoginSuccessfull(){

//here do whatever you want to do .

}

每当用户尝试登录时,都会调用此登录成功,因此不会再次初始化
pinAttempts
,一旦您决定取消阻止,用户将该
pinAttempts
再次计数到
0

1。)不要使pinAttempts=0;如果每次用户按enter键时都调用它,请尝试{}!2)如果(pInTrase= 3){…返回一个;返回,不做其他代码}@ Kelv如果一个答案对你有用,考虑接受它。