“Devil组”引证GPT后的撰写:
import java.util.Scanner;
public class LoginSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("欢迎使用学生信息管理系统");
System.out.println("登录—————1");
System.out.println("退出—————2");
int choice = scanner.nextInt();
if (choice == 1) {
int result = checkPSW();
if (result > 0) {
System.out.println("登录成功");
// 这里可以添加进入菜单的代码
} else if (result == 0) {
// 用户输入了#,返回到登录界面
continue;
} else {
System.out.println("你的密码输入错误已经超过三次");
System.exit(0);
}
} else if (choice == 2) {
System.exit(0);
} else {
System.out.println("输入无效,请重新输入");
}
}
}
public static int checkPSW() {
Scanner scanner = new Scanner(System.in);
int count = 0;
while (count < 3) {
System.out.print("请输入你的密码,#号返回:");
String input = scanner.nextLine();
if (input.equals("#")) {
return 0;
} else if (input.equals(getStudentID().substring(4))) {
return 1;
} else {
System.out.println("密码错误,请重新输入");
count++;
}
}
return -1;
}
public static String getStudentID() {
// 这里可以实现获取学号的代码
return "1234567890";
}
}
用了一个while循环,不断显示登录与退出的界面,并读入用户的选择。如果用户选择了登录,就调用checkPSW()方法进行密码检验。如果检验通过,就输出“登录成功”,可以进入菜单的代码可以在此处添加。如果输入了#,就返回到登录界面。如果密码输入错误,则提示用户重新输入,最多只允许输错三次,超过三次则退出程序。
- checkPSW()方法实现了密码检验功能。在这个方法中,用了一个计数器count来记录用户输入密码错误的次数。在while循环中,首先提示用户输入密码,并读入用户输入的字符串。如果用户输入了#,就返回0。如果用户输入的是正确的密码,也就是学号的后六位,就返回1。如果用户输入的是错误的密码,就提示用户重新输入,并让计数器加1。如果计数器超过了3,就输出错误信息并退出程序。
- 这个程序中还有一个getStudentID()方法,用于获取学号。由于题目中没有明确给出获取学号的方法,只是简单地返回一个字符串“1234567890”