赞
踩
通过学习Java基础,采取循环,判断等语句来实现一个简单的学生管理系统
public class Student { //学号 private String sid; //姓名 private String name; //年龄 private String age; //居住地 private String address; public Student() { } public Student(String sid, String name, String age, String address) { this.sid = sid; this.name = name; this.age = age; this.address = address; } public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } 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; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
public class StudentManager { public static void main(String[] args) { ArrayList<Student> arrayList = 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(); switch (line) { case "1": addStudent(arrayList); break; case "2": deleteStudent(arrayList); break; case "3": updateStudent(arrayList); break; case "4": findAllStudent(arrayList); break; case "5": System.out.println("谢谢使用"); System.exit(0);//JVM(Java虚拟机)退出 default: System.out.println("你输入的信息有误,现在重新进入系统"); } } } //添加学生 public static void addStudent(ArrayList<Student> arrayList) { Scanner sc = new Scanner(System.in); String sid; while (true){ System.out.println("请输入学生学号: "); sid = sc.nextLine(); boolean flag = isUsed(arrayList, sid); if (flag){ System.out.println("你输入的学号已经被使用,请重新输入!"); }else { break; } } System.out.println("请输入学生姓名: "); String name = sc.nextLine(); System.out.println("请输入学生年龄: "); String age = sc.nextLine(); System.out.println("请输入学生居住地: "); String address = sc.nextLine(); //创建学生对象,并将键盘录入的数据赋值给学生对象的成员变量 Student s = new Student(); s.setSid(sid); s.setName(name); s.setAge(age); s.setAddress(address); //将学生对象添加到集合中 arrayList.add(s); //给出添加成功的提示信息 System.out.println("添加学生成功"); } //判断学号是否被使用 public static boolean isUsed(ArrayList<Student> arrayList,String sid){ boolean flag = false; for (int i = 0;i<arrayList.size();i++){ Student student = arrayList.get(i); if (student.getSid().equals(sid)){ flag = true; break; } } return flag; } //删除学生 public static void deleteStudent(ArrayList<Student> arrayList){ int index = -1; Scanner sc = new Scanner(System.in); System.out.println("请输入你要删除学生的学号: "); String sid = sc.nextLine(); for (int i = 0;i<arrayList.size();i++){ Student student = arrayList.get(i); if (student.getSid().equals(sid)){ index = i; break; } } if (index == -1){ System.out.println("输入的学生" + sid + "学号不存在"); }else { arrayList.remove(index); System.out.println("删除学生信息成功"); } } //修改学生信息 public static void updateStudent(ArrayList<Student> arrayList){ int index = -1; Scanner sc = new Scanner(System.in); System.out.println("请输入你需要修改的学生的学号: "); String sid = sc.nextLine(); System.out.println("请输入学生新姓名: "); String name = sc.nextLine(); System.out.println("请输入学生新年龄: "); String age = sc.nextLine(); System.out.println("请输入学生新居住地: "); String address = sc.nextLine(); Student student = new Student(); student.setSid(sid); student.setName(name); student.setAge(age); student.setAddress(address); for (int i = 0;i<arrayList.size();i++){ Student student1 = arrayList.get(i); if (student1.getSid().equals(sid)){ index = i; break; }else { System.out.println("没有学号为" + sid + "的学生,重新进入修改学生信息状态"); updateStudent(arrayList); } } if (index == -1){ System.out.println("没有学号为" + sid + "的学生,重新进入修改学生信息状态"); }else { arrayList.set(index,student); System.out.println("修改学生信息成功"); } } //查看全部学生 public static void findAllStudent(ArrayList<Student> arrayList) { if (arrayList.size() == 0) { System.out.println("暂无学生信息,请输入后再查询!"); } else { System.out.println("学号\t\t\t姓名\t\t\t年龄\t\t\t居住地"); for (int i = 0; i < arrayList.size(); i++) { Student student = arrayList.get(i); System.out.println(student.getSid() + "\t" + student.getName() + "\t\t\t" + student.getAge() + "岁\t\t" + student.getAddress()); } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。