[英]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.