import java.util.ArrayList; import java.util.Scanner; public class StudentGL { public static void main(String[] args) { ArrayList<Student> al = new ArrayList(); //创建集合对象用来装学生 while (true){ System.out.println("----学生管理系统----"); System.out.println("输入1添加学生"); System.out.println("输入2删除学生"); System.out.println("输入3修改学生"); System.out.println("输入4查询学生"); System.out.println("输入5退出系统"); System.out.println("请输入:"); Scanner sc = new Scanner(System.in); String line = sc.nextLine(); //录入用户选择 //Student s =new Student(); switch (line){ //输入1添加学生 case "1": System.out.println("请输入学生编号:"); String id = sc.nextLine(); System.out.println("请输入学生姓名:"); String name = sc.nextLine(); System.out.println("请输入学生年龄:"); String age = sc.nextLine(); Student s =new Student(); //创建学生对象 s.setId(id); s.setName(name); s.setAge(age); al.add(s); break; //输入2删除学生 case "2": System.out.println("请输入学生编号:"); String id2 = sc.nextLine(); for (int i = 0; i < al.size(); i++) { Student ss = al.get(i); if (id2.equals(ss.getId())) { al.remove(i); } } break; //输入3修改学生 case "3": System.out.println("请输入学生编号:"); String num = sc.nextLine(); for (int i = 0; i < al.size(); i++) { Student ss = al.get(i); if (num.equals(ss.getId())){ System.out.println("输入0撤销;输入1修改编号;输入2修改姓名;输入3修改年龄。" +" "+ "请输入要修改的项号:"); String xiang = sc.nextLine(); switch (xiang){ case "0": break; case "1": System.out.println("请输入学生编号:"); String id3 = sc.nextLine(); ss.setId(id3); break; case "2": System.out.println("请输入学生姓名:"); String name3 = sc.nextLine(); ss.setName(name3); break; case "3": System.out.println("请输入学生年龄:"); String age3 = sc.nextLine(); ss.setAge(age3); break; } al.set(i, ss); } } break; //输入4查询学生 case "4": for (int i = 0; i < al.size(); i++) { Student ss = al.get(i); System.out.println(ss.getId() + ss.getName() + ss.getAge()); } break; //输入5退出系统 case "5": System.out.println("谢谢使用!"); System.exit(0); break; } } } } class Student{ private String id; private String name; private String age; public Student() { } public Student(String id, String name, String age) { this.id = id; this.name = name; this.age = age; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }