1.學(xué)生管理系統(tǒng)介紹
? ?Java程序設(shè)計(jì)語言完成控制臺(tái)簡(jiǎn)單學(xué)生管理系統(tǒng),創(chuàng)建集合對(duì)象實(shí)現(xiàn)學(xué)生信息的存儲(chǔ),利用集合方法實(shí)現(xiàn)信息的輸出,創(chuàng)建scanner對(duì)象完成數(shù)據(jù)輸入,分別完成學(xué)生信息新增、刪除、修改、查看等方法,并在主方法中調(diào)用完成學(xué)生管理系統(tǒng)的所有功能。
運(yùn)行效果:
?
?
2.學(xué)生管理系統(tǒng)實(shí)現(xiàn)思路
-
定義學(xué)生類
-
主界面的代碼編寫
-
添加學(xué)生的代碼編寫
-
查看學(xué)生的代碼編寫
-
刪除學(xué)生的代碼編寫
-
修改學(xué)生的代碼編寫
3.定義學(xué)生類
學(xué)生類:Student
成員變量:
-
學(xué)號(hào):
sid
-
姓名:name
-
年齡:age
-
居住地:address
構(gòu)造方法:
-
無參構(gòu)造
-
帶四個(gè)參數(shù)的構(gòu)造
成員方法:文章來源:http://www.zghlxwxcb.cn/news/detail-542477.html
-
每個(gè)成員變量對(duì)應(yīng)給出get/set方法文章來源地址http://www.zghlxwxcb.cn/news/detail-542477.html
//學(xué)生類 //Alt+insert 構(gòu)造器快捷鍵 public class Student { private String sid; //學(xué)號(hào) 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; } }
4.主界面的代碼編寫
public class StudentManger{} /* 1.用輸出語句完成主界面的編寫 2.用scanner實(shí)現(xiàn)鍵盤錄入數(shù)據(jù) 3.用switch語句完成操作選擇 4.用循環(huán)完成再次回到主界面 */ public class StudentManage { public static void main(String[] args) { ArrayList<Student> array = new ArrayList<Student>(); //創(chuàng)建集合對(duì)象,用于存儲(chǔ)學(xué)生信息 while(true){ //使用循環(huán)完成再次回到主界面 //用輸出語句完成主界面的編寫 System.out.println("---------歡迎來到學(xué)生信息管理系統(tǒng)--------"); System.out.println("1 添加學(xué)生"); System.out.println("2 刪除學(xué)生"); System.out.println("3 修改學(xué)生"); System.out.println("4 查看所有學(xué)生"); System.out.println("5 退出"); System.out.println("請(qǐng)輸入你的選擇:"); Scanner sc = new Scanner(System.in); //使用Scanner實(shí)現(xiàn)鍵盤錄入數(shù)據(jù) String line = sc.nextLine(); switch (line) { //用switch語句完成操作的選擇 case "1": addStudent(array); break; case "2": deleteStudent(array); break; case "3": updateStudent(array); break; case "4": findAllStudent(array); break; case "5": System.out.println("謝謝使用"); System.exit(0); //break; } } }
5.添加學(xué)生代碼編寫
//定義一個(gè)方法,用于添加學(xué)生信息 public static void addStudent(ArrayList<Student> array) { //鍵盤錄入學(xué)生對(duì)象所需要的數(shù)據(jù),顯示提示信息,提示要輸入何種信息 Scanner sc = new Scanner(System.in); String sid; //為了讓程序回到這里,使用循環(huán)處理 while(true) { System.out.println("請(qǐng)輸入學(xué)生學(xué)號(hào):"); sid = sc.nextLine(); boolean flag = isUsed(array, sid); if (flag) { System.out.println("你輸入的學(xué)號(hào)已經(jīng)被使用,請(qǐng)重新輸入"); }else{ break; } } System.out.println("請(qǐng)輸入學(xué)生姓名:"); String name = sc.nextLine(); System.out.println("請(qǐng)輸入學(xué)生年齡:"); String age = sc.nextLine(); System.out.println("請(qǐng)輸入學(xué)生地址:"); String address = sc.nextLine(); //創(chuàng)建學(xué)生對(duì)象 Student s = new Student(); s.setSid(sid); s.setName(name); s.setAge(age); s.setAddress(address); //將學(xué)生對(duì)象添加到集合中 array.add(s); //給出添加成功提示 System.out.println("添加學(xué)生成功"); } //定義一個(gè)方法,判斷學(xué)號(hào)是否被使用 public static boolean isUsed(ArrayList<Student> array,String sid){ //如果與集合中的某一個(gè)學(xué)生學(xué)號(hào)相同,返回true;如果都不同,返回false boolean flag = false ; for (int i =0;i <array.size();i++){ Student s = array.get(i); if (s.getSid().equals(sid)){ flag = true; break; } } return flag; }
6.查看學(xué)生代碼編寫
public static void findAllStudent(ArrayList<Student> array){ //判斷集合中是否有數(shù)據(jù),如果沒有顯示提示信息 if (array.size()==0){ System.out.println("無信息,請(qǐng)先添加信息再查詢"); return; } //顯示表頭信息 System.out.println("學(xué)號(hào)\t\t姓名\t\t年齡\t\t居住地"); //將集合中的數(shù)據(jù)取出按照對(duì)應(yīng)格式顯示學(xué)生信息,年齡顯示補(bǔ)充“歲” for (int i=0;i<array.size();i++){ Student s = array.get(i); System.out.println(s.getSid() + "\t\t\t" + s.getName() + "\t\t" + s.getAge() + "歲" + "\t\t" + s.getAddress()); } }
7.刪除學(xué)生代碼編寫
public static void deleteStudent(ArrayList<Student> array){ //鍵盤鍵入要?jiǎng)h除的學(xué)生學(xué)號(hào),顯示提示信息 Scanner sc = new Scanner(System.in); System.out.println("請(qǐng)輸入要?jiǎng)h除學(xué)生的學(xué)號(hào)"); String sid = sc.nextLine(); //遍歷集合將對(duì)應(yīng)學(xué)生對(duì)象從集合中刪除 int index = -1; for (int i =0; i<array.size();i++){ Student s = array.get(i); if (s.getSid().equals(sid)){ index = i; break; } } if (index == -1){ System.out.println("該信息不存在,請(qǐng)重新輸入"); }else{ array.remove(index); System.out.println("刪除學(xué)生信息成功"); } }
8.修改學(xué)生代碼編寫
public static void updateStudent(ArrayList<Student> array){ //鍵盤錄入要修改的學(xué)生學(xué)號(hào) Scanner sc = new Scanner(System.in); System.out.println("請(qǐng)輸入修改學(xué)生的學(xué)號(hào)"); String sid = sc.nextLine(); //鍵盤輸入要修改的學(xué)生信息 System.out.println("請(qǐng)輸入學(xué)生新姓名"); String name = sc.nextLine(); System.out.println("請(qǐng)輸入學(xué)生新年齡"); String age = sc.nextLine(); System.out.println("請(qǐng)輸入學(xué)生新地址"); String address = sc.nextLine(); //創(chuàng)建學(xué)生對(duì)象 Student s = new Student(); s.setSid(sid); s.setName(name); s.setAge(age); s.setAddress(address); //遍歷集合修改對(duì)應(yīng)的學(xué)生信息 for (int i = 0; i<array.size();i++){ Student student = array.get(i); if (student.getSid().equals(sid)) { array.set(i, s); break; } } //給出修改成功提示 System.out.println("修改成功"); } }
到了這里,關(guān)于Java學(xué)生管理系統(tǒng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!