有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

身份验证我如何创建一个登录屏幕,允许输入一个通用密码,但在三个三之后程序必须关闭(Java)

信息需要3次尝试,然后程序关闭。 每个用户使用相同的密码
密码设置得太高,密码=12345678。 用户数据是从GUI组件中提取的

我试过的代码:

String password = "12345678";
    
String userInput = String.valueOf(jPasswordField1.getPassword()); 
int length = userInput.length(); 
if (userInput.equals(password)) { 
    this.dispose(); 
    new INPUT().setVisible(true); 
} 
else if (!userInput.equals(password)) { 
    if (length > 9) { 
        lblValid.setText("To many characters"); 
        tries++; 
    } 
    else if (length < 8) { 
        lblValid.setText("To little characters"); 
        tries++; 
    } 
} 
if (tries == 3) { 
    System.exit(0); 
}

共 (1) 个答案

  1. # 1 楼答案

    您可以使用扫描仪和while循环来实现:

        Scanner scanner = new Scanner(System.in);
        int tries=0;
        String password = "asdf123";//DO NOT USE THIS PASSWORD
        while(tries<3) {
            System.out.print("\nEnter password: ");
            String passwordattempt = scanner.nextLine();
            if(!passwordattempt.equals(password)) {
                System.out.println("Wrong password");
                tries++;
            }else {
                System.out.println("Correct");
                break;
            }
        }
        scanner.close();