關鍵技術:循環,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中文網其他相關文章!