【Java基础】--学生管理系统

2023-12-23 06:53:26
题目概述:

需求:采用控制台的形式书写学生管理系统,如下图所示:

代码如下:

public class 学生管理系统_01 {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
      while (true) {
            System.out.println("--------欢迎来到Boss学生管理系统--------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.查询所有学生");
            System.out.println("6.退出");
            System.out.println("请输入你的选择:");
            String s = scanner.next();
            loop:switch (s) {
                case "1":
//                    System.out.println("请输入学生的id:");
//                    Integer id = scanner.nextInt();
//                    boolean mark = true;
//                    for (int i = 0; i < students.size(); i++) {
//                        Student student = students.get(i);
//                        if (student.getId().equals(id)) {
//                            System.out.println("id重复,请重新输入!");
//                            mark = false;
//                            break;
//                        }
//                    }
//                    if (mark) {
//                        System.out.println("请输入学生的name:");
//                        String name = scanner.next();
//                        System.out.println("请输入学生的age:");
//                        int age = scanner.nextInt();
//                        System.out.println("请输入学生的address:");
//                        String address = scanner.next();
//                        Student stu = new Student(id, name, age, address);
//                        students.add(stu);
//                        System.out.println("添加学生成功!");
//                    }
                    System.out.println("请输入学生的id:");
                    Integer id = scanner.nextInt();
                    for (int i = 0; i < students.size(); i++) {
                        Student student = students.get(i);
                        if (student.getId().equals(id)) {
                            System.out.println("id重复,请重新输入!");
                            break loop;
                        }
                    }
                    System.out.println("请输入学生的name:");
                    String name = scanner.next();
                    System.out.println("请输入学生的age:");
                    int age = scanner.nextInt();
                    System.out.println("请输入学生的address:");
                    String address = scanner.next();
                    Student stu = new Student(id, name, age, address);
                    students.add(stu);
                    System.out.println("添加学生成功!");
                    break;
                case "2":
                    System.out.println("请输入你要删除学生的id:");
                    Integer sid = scanner.nextInt();
                    boolean flag = delectStu(students, sid);
                    if (!flag) {
                        System.out.println("id不存在,请重新输入!");
                    }
                    break;
                case "3":
                    System.out.println("请输入你要修改学生的id:");
                    Integer sid1 = scanner.nextInt();
                    boolean flag1 = updateStu(students, sid1, scanner);
                    if (!flag1) {
                        System.out.println("id不存在,请重新输入!");
                    }
                    break;
                case "4":
                    System.out.println("请输入你要查询学生的id:");
                    Integer sid2 = scanner.nextInt();
                    boolean flag2 = selectStu(students, sid2);
                    if (!flag2) {
                        System.out.println("当前无学生信息,请添加后再次查询!");
                    }
                    break;
                case "5":
                    if (students.size() != 0) {
                        selectStuAll(students);
                    } else {
                        System.out.println("当前学生信息为空,请进行添加!");
                    }
                    break;
                case "6":
                    System.exit(0);
                    break;
            }
        }
    }


    public static boolean delectStu(ArrayList<Student> students, Integer sid) {
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            if (student.getId().equals(sid)) {
                students.remove(i);
                System.out.println("删除成功!");
                return true;
            }
        }
        return false;
    }

    public static boolean updateStu(ArrayList<Student> students, Integer sid, Scanner scanner) {
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            if (student.getId().equals(sid)) {
                System.out.println("请输入你要修改学生的name:");
                String name1 = scanner.next();
                System.out.println("请输入你要修改学生的age:");
                int age1 = scanner.nextInt();
                System.out.println("请输入你要修改学生的address:");
                String address1 = scanner.next();
                student.setName(name1);
                student.setAge(age1);
                student.setAddress(address1);
                students.set(i, student);
                System.out.println("修改成功!");
                return true;
            }
        }
        return false;
    }


    public static boolean selectStu(ArrayList<Student> students, Integer sid) {
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            if (student.getId().equals(sid)) {
                System.out.println("id=" + student.getId() + " name=" + student.getName() + " age=" + student.getAge() + " address=" + student.getAddress());
                return true;
            }
        }
        return false;
    }

    public static void selectStuAll(ArrayList<Student> students) {
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            System.out.println("id=" + student.getId() + " name=" + student.getName() + " age=" + student.getAge() + " address=" + student.getAddress());
        }
    }
}
总结:

在面对如下代码:switch中嵌套for循环,显然一个break是跳不出去switch的,这样就出现了一个问题,程序运行时,当id重复后break只会跳出for循环,这样程序还会继续往下走,显然逻辑这样做的逻辑是不正确的。那么要怎么来解决呢?这里是我想出来的办法:

第一个办法:在for循环外设置一个mark标记,当id重复后来改变mark的值,从而来确定循环后的代码要不要执行

第二个办法:写一个方法,在case中直接调用方法,这样也能解决该问题。(具体实现如我上述代码的修、查、删功能)

第三个办法:为switch起一个名字loop,当id相同时直接break loop,也能直接跳出switch。(具体实现如我上述代码的增加功能)?

                    case "1":
?                   System.out.println("请输入学生的id:");
                    Integer id = scanner.nextInt();
                    for (int i = 0; i < students.size(); i++) {
                        Student student = students.get(i);
                        if (student.getId().equals(id)) {
                            System.out.println("id重复,请重新输入!");
                            break;
                        }
                    }
                    System.out.println("请输入学生的name:");
                    String name = scanner.next();
                    System.out.println("请输入学生的age:");
                    int age = scanner.nextInt();
                    System.out.println("请输入学生的address:");
                    String address = scanner.next();
                    Student stu = new Student(id, name, age, address);
                    students.add(stu);
                    System.out.println("添加学生成功!");
?                   break;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

文章来源:https://blog.csdn.net/m0_65368672/article/details/135136651
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。