关键技术:循环,switch条件判断,创建对象,ArrayList()动态数组或集合,private
系统功能:
1、添加学生 (学号,姓名,年龄,居住地)以学号为基准进行遍历,如果学号已存在,则重新输入
2、修改学生 同样以学号为判断依据,输入要修改的学生学号,依次重新输入姓名,年龄,居住地,如果学号输入错误,则提示重新输入
3、删除学生 以学号为基准进行判断,学号不存在则进行提示;学号存在,则删除改学号所包含的全部信息
4、查看全部学生信息,采用循环遍历
5、退出程序 如果不选择,则程序回到主页面
创建学生类
package student_status_management_system; public class Student { private String student_number; private String name; private String age; private String address; public Student() { } public Student(String student_number, String name, String age, String address) { this.student_number = student_number; this.name = name; this.age = age; this.address = address; } public String getStudent_number() { return student_number; } public void setStudent_number(String student_number) { this.student_number = student_number; } 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 static boolean repetition(ArrayList<Student>array,String studentnumber){ boolean flag = false; for (int i = 0;i<array.size();i++){ Student s = array.get(i); if (s.getStudent_number().equals(studentnumber)){ flag = true; break; } } return flag; }
添加学生
public static void addStudent(ArrayList<Student> array) { String studentnumber; Scanner sc = new Scanner(System.in); while (true) { System.out.println("请输入学生学号:"); studentnumber = sc.nextLine(); boolean flag = repetition(array, studentnumber); 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.setStudent_number(studentnumber); s.setName(name); s.setAge(age); s.setAddress(address); //将信息添加到集合 array.add(s); System.out.println("添加成功!"); }
修改学生信息
public static void updateStudent(ArrayList<Student> array) { int index = -1; Scanner sc = new Scanner(System.in); System.out.println("请输入你要修改的学生的学号:"); String id = sc.nextLine(); for (int i = 0; i < array.size(); i++) { Student student = array.get(i); if (student.getStudent_number().equals(id)) { index = i; break; } } if (index == -1) { System.out.println("该学号不存在!"); } else { 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.setStudent_number(id); s.setName(name); s.setAge(age); s.setAddress(address); array.set(index, s); System.out.println("修改成功!"); } }
删除学生
public static void deleteStudent(ArrayList<Student> array) { int index = -1; //定义索引 Scanner sc = new Scanner(System.in); System.out.println("请输入你要删除的学生的学号:"); String id = sc.nextLine(); for (int i = 0; i < array.size(); i++) { Student s = array.get(i); if (s.getStudent_number().equals(id)) { index = i; break; } } if (index == -1) { System.out.println("该学号不存在!"); } else { array.remove(index); System.out.println("删除成功!"); } }
查看所有学生
public static void findAllStudent(ArrayList<Student> array) { //显示表头 if (array.size() == 0) { System.out.println("你还没有输入信息!"); return;//阻止程序往下执行 } System.out.println("学号\t\t\t\t姓名\t\t年龄\t\t\t居住地"); for (int i = 0; i < array.size(); i++) { Student s = array.get(i); System.out.println(s.getStudent_number() + "\t\t" + s.getName() + "\t\t" + s.getAge() + "岁" + "\t\t" + s.getAddress()); } }
main方法
public static void main(String[] args) { //创建集合,储存学生信息 ArrayList<Student> 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 退出系统"); //主界面 Scanner sc = new Scanner(System.in); System.out.println("请输入你要进行的操作(数字序号):"); String line = sc.nextLine(); switch (line) { case "1": addStudent(array); break; case "2": updateStudent(array); break; case "3": deleteStudent(array); break; case "4": findAllStudent(array); break; case "5": System.out.println("再见!"); System.exit(0);//JVM退出 } } }
效果演示
添加学生
修改学生
删除学生
退出程序
没有添加学生信息时查看学生
以上是Java编写控制台学生学籍管理系统实现详解的详细内容。更多信息请关注PHP中文网其他相关文章!