简体   繁体   English

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

[英]how do i create a login screen that allows for a common password to be entered but after the 3 threes the program must close (Java )

Information needed 3 tries then program closes.需要的信息 3 次尝试然后程序关闭。 The same password is used for every user.每个用户都使用相同的密码。
The password is set too password = 12345678. User data is extracted from a GUI component密码也设置了 password = 12345678. 用户数据是从一个 GUI 组件中提取的

Code I've tried:我试过的代码:

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

You can use Scanner along with a while loop to achieve it:您可以使用 Scanner 和 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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为在Java程序中输入的值创建占位符? - How can I create placeholders for values I entered in a Java program? 如何使用 3 个密码创建登录 Java 程序尝试检查 - how to create a login Java Program with 3 password Attempt check 如何验证输入的密码是否与输入的电子邮件相关联? - How do I verify password entered is the password associated with entered email? 我需要创建一个程序,允许用户输入任何字符串。 程序必须确定字符串中有多少个元音和单词 - I need to create a program that allows a user to input any string. The program must determine how many vowels and words are within the string 如何在Java中创建屏幕键盘? - How do I create an on screen keyboard in java? 如何解密和加密Java程序的Joomla用户密码? - How do I decrypt and encrypt Joomla user password for Java program? 我如何让我的Java程序等到在文本字段中输入值 - How do i make my java program wait until there is entered a value into the textfield 如何使用Java函数编写程序判断用户输入的4个整数是正方形还是长方形? - How do I use Java functions to write a program that determines whether 4 integers entered by a user form a square or a rectangle? 如果用户输入错误密码超过3次,如何阻止用户在servlet jsp程序中登录 - how to block a user login in servlet jsp program if he/she entered wrong password more than 3 times 在java中捕获IOException后如何关闭文件? - How do I close a file after catching an IOException in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM