目录
1.学生类
2.测试类
2.1主界面代码
2.2添加学生方法
2.3删除学生方法
2.4修改学生方法
2.5查看所有学生方法
3.运行效果
1.学生类
public class Student { private String name; private String age; private String studentId; private String address; public Student(){} public Student(String name,String age,String studentId,String address){ this.name =name; this.age = age; this.studentId = studentId; this.address = address; } public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setAge(String age){ this.age = age; } public String getAge(){ return age; } public void setStudentId(String studentId){ this.studentId = studentId; } public String getStudentId(){ return studentId; } public void setAddress(String address){ this.address = address; } public String getAddress(){ return address; } }2.测试类 2.1主界面代码
public class StudentManager { public static void main(String[] args) { ArrayList2.2添加学生方法array = 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); int a = sc.nextInt(); switch (a) { case 1: addStudent(array); break; case 2: delStudent(array); break; case 3: modifyStudent(array); break; case 4: scan(array); break; case 5: System.out.println("谢谢使用"); System.exit(0); } } }
//添加学生 public static void addStudent(ArrayList2.3删除学生方法array) { Scanner sc = new Scanner(System.in); System.out.print("姓名:"); String name = sc.nextLine(); System.out.print("学号:"); String studentId = sc.nextLine(); System.out.print("年龄:"); String age = sc.nextLine(); System.out.print("地址:"); String address = sc.nextLine(); Student s = new Student(); s.setName(name); s.setStudentId(studentId); s.setAge(age); s.setAddress(address); array.add(s); System.out.println("添加成功"); }
//删除学生 public static void delStudent(ArrayList2.4修改学生方法array) { Scanner sc = new Scanner(System.in); System.out.print("请输入需要删除的学生学号:"); String studentId = sc.nextLine(); for (int i = 0; i < array.size(); i++) { Student s = array.get(i); if (s.getStudentId().equals(studentId)) { array.remove(i); break; } else { System.out.println("您输入的学号有误"); } } }
//修改学生 public static void modifyStudent(ArrayList2.5查看所有学生方法array) { Scanner sc = new Scanner(System.in); System.out.print("请输入需要修改学生的学号:"); String studentId = sc.nextLine(); for (int i = 0; i < array.size(); i++) { Student s = array.get(i); if (s.getStudentId().equals(studentId)) { System.out.print("请输入新姓名:"); String name = sc.nextLine(); System.out.print("请输入新年龄:"); String age = sc.nextLine(); System.out.print("请输入新地址:"); String address = sc.nextLine(); s.setName(name); s.setAddress(address); s.setAge(age); } else { System.out.println("输入的学号有误"); } } }
//查看所有学生 public static void scan(ArrayList3.运行效果array) { if(array.size()==0){ System.out.println("无信息,请先添加信息再查询"); return; } System.out.println("学号t姓名t年龄t居住地"); for (int i = 0; i < array.size(); i++) { Student s = array.get(i); System.out.println(s.getName() + "t" + s.getStudentId() + "t" + s.getAge() + "岁t" + s.getAddress()); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)