java java运行 编程语言

编写程序实现如下功能:

显示一个软件的登录页面和菜单对应的选项。

    登录界面示例如下:

    ~~~~~~~~~~~~

    欢迎使用学生信息管理系统

    ~~~~~~~~~~~~

    登录—————1

    退出—————2

    ~~~~~~~~~~~~

    当用户输入1时显示请输入密码,如果输入的密码为123456,则输出文字“登录成功”,否则输出文字“密码错误”。

在以上基础上,修改其代码使其实现

  1. 在显示“登录与退出”界面时,读入用户输入的数值,如果是1,在输出如下界面提示用户输入密码,如果是2则退出程序;

     请输入你的密码,#号返回:
    
  2. 在输入密码界面下,如果密码输入错误则提示用户密码错误,继续输入密码,但是输错三次后,则退出程序;如果输入“#",则返回到登录界面,继续提示用户输入登录还是退出;如果输入的为你学号的后6位,则为正确的密码;

  3. 密码检验功能必须通过调用方法实现,方法名为checkPSW,返回值为整数:

如果返回值>0,表示密码输入正确;

如果返回值=0,表示密码用户输入了#;

如果返回值<0,表示密码输入密码次数超过三次,在屏幕上输出“你的密码输入错误已经超过三次”,并退出程序;

参考 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;
        }
    }
}

“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”