参考 GPT
import java.util.Scanner;
public class Login {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String stdPwd = "123456";
while (true) {
System.out.println("~~~~~~~~~~~~");
System.out.println("欢迎使用学生信息管理系统");
System.out.println("~~~~~~~~~~~~");
System.out.println("登录—————1");
System.out.println("退出—————2");
System.out.println("~~~~~~~~~~~~");
String choice = scanner.nextLine();
if ("1".equals(choice)) {
int pwdCount = 0;
while (true) {
System.out.println("请输入你的密码,#号返回:");
String pwd = scanner.nextLine();
pwdCount++;
int checkResult = checkPSW(pwd, stdPwd);
if (checkResult > 0) {
System.out.println("登录成功");
break;
} else if (checkResult == 0) {
break;
} else {
if (pwdCount >= 3) {
System.out.println("你的密码输入错误已经超过三次");
System.exit(0);
} else {
System.out.println("密码错误,请重试");
}
}
}
} else if ("2".equals(choice)) {
break;
} else {
System.out.println("无效选择,请重新输入");
}
}
}
public static int checkPSW(String pwd, String stdPwd) {
if ("#".equals(pwd)) {
return 0;
} else if (pwd.equals(stdPwd)) {
return 1;
} else {
return -1;
}
}
}