java学生管理系统
生活随笔
收集整理的這篇文章主要介紹了
java学生管理系统
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
以下記錄來自一個(gè)菜鳥,請(qǐng)大佬們見諒
2019.4.6 16:49,已經(jīng)在電腦前坐了將近五個(gè)小時(shí)的我將個(gè)人第一個(gè)java優(yōu)化系統(tǒng)調(diào)試完成,五個(gè)小時(shí),只是將已經(jīng)寫好的系統(tǒng)進(jìn)行優(yōu)化,起初開始優(yōu)化的時(shí)候覺得這簡(jiǎn)直是個(gè)無底洞,要考慮的東西太多太細(xì)(忽然想起一位java老師曾說過,“永遠(yuǎn)不要相信你的用戶都是正常人”,言外之意就是要考慮到用戶各種各樣稀奇古怪的操作),而且不同類之間的分工很難協(xié)調(diào),初步估算工程量比我原本寫出系統(tǒng)的量還大,在這五個(gè)小時(shí)大腦周圍一直有幾個(gè)聲音圍著我轉(zhuǎn):“睡一會(huì)吧”、“去##的**系統(tǒng)”、“這##是人干的事嗎”。但當(dāng)最后一個(gè)javac stuSystem.java后沒有報(bào)錯(cuò),所有的一切都變成了一個(gè)聲音:“值得!”
以下附上(1)第一次實(shí)現(xiàn)功能的代碼
(2)優(yōu)化后的代碼
(1)
import java.util.Scanner; class Student {String num;String name;String gender;byte age;byte grade;}class main{public static void main(String[] args ){System.out.println("歡迎使用學(xué)生信息管理系統(tǒng)");Student stu[] = new Student[10];int dataIndex = 0;Scanner scanner = new Scanner(System.in);while(true){System.out.println("請(qǐng)選擇操作");System.out.println("0:退出系統(tǒng)");System.out.println("1:輸入學(xué)生信息");System.out.println("2:查詢學(xué)生信息");System.out.println("3:修改學(xué)生信息");System.out.println("4:刪除學(xué)生信息");//int opIndex = scan.nextInt();int opIndex = scanner.nextInt();switch(opIndex){case 0:System.exit(0);///退出系統(tǒng)case 1:Scanner scan = new Scanner(System.in);Student stud = new Student();System.out.println("請(qǐng)輸入學(xué)生的學(xué)號(hào)");stud.num = scan.next() ;///重點(diǎn):scanner 的使用System.out.println("請(qǐng)輸入學(xué)生的姓名");stud.name = scan.next() ;///加括號(hào)System.out.println("請(qǐng)輸入學(xué)生的性別");stud.gender = scan.next() ;System.out.println("請(qǐng)輸入學(xué)生的年齡");stud.age = scan.nextByte() ;System.out.println("請(qǐng)輸入學(xué)生的成績(jī)");stud.grade = scan.nextByte() ;stu[dataIndex] = stud;dataIndex++; break;case 2:System.out.println("請(qǐng)輸入要查詢學(xué)生的姓名");Scanner sc = new Scanner(System.in);String nam = sc.next();int numb = -1;for(int i = 0; i <dataIndex ; i++){if(stu[i].name .equals(nam)){numb = i;}}if(numb >=0){System.out.println("該學(xué)生的學(xué)號(hào)為:" + stu[numb].num);System.out.println("該學(xué)生的性別為:" + stu[numb].gender); System.out.println("該學(xué)生的年齡為:" + stu[numb].age);System.out.println("該學(xué)生的成績(jī)?yōu)?#xff1a;" + stu[numb].grade);}else{System.out.println("不存在該學(xué)生!!");}break;case 3:System.out.println("請(qǐng)輸入要修改信息學(xué)生的姓名");Scanner sc1 = new Scanner(System.in);//sc不能重復(fù)定義String nam1 = sc1.next();///nam不能重復(fù)定義int numb1 = -1;for(int i = 0; i <dataIndex ; i++){if(stu[i].name .equals(nam1)){numb1 = i;} }if(numb1 >= 0){System.out.println("請(qǐng)輸入學(xué)生的學(xué)號(hào)");stu[numb1].num = sc1.next() ;System.out.println("請(qǐng)輸入學(xué)生的姓名");stu[numb1].name = sc1.next() ;System.out.println("請(qǐng)輸入學(xué)生的性別");stu[numb1].gender = sc1.next() ;System.out.println("請(qǐng)輸入學(xué)生的年齡");stu[numb1].age = sc1.nextByte() ;System.out.println("請(qǐng)輸入學(xué)生的成績(jī)");stu[numb1].grade = sc1.nextByte() ;}else{System.out.println("不存在該學(xué)生!!");}break;case 4:System.out.println("請(qǐng)輸入要?jiǎng)h除信息學(xué)生的姓名");Scanner sc2 = new Scanner(System.in);String nam2 = sc2.next();int numb2 = -1;for(int i = 0; i <dataIndex ; i++){if(stu[i].name .equals(nam2)){numb2 = i;} if(numb2 >= 0){for(i= numb2;i<dataIndex;i++){stu[i] = stu[i+1];}dataIndex --;}else{System.out.println("不存在該學(xué)生!!");}}}///數(shù)組的增長(zhǎng)if(dataIndex == stu.length){Student newstu[] = new Student[stu.length + stu.length>>1];for(int i = 0;i < stu.length ;i++){newstu[i] = stu[i]; }stu = newstu;}}} }///成員變量和局部變量/***************************************** 設(shè)計(jì)思路 1、輸入: (1)無限循環(huán)輸入 (2)根據(jù)用戶操作停止輸入 (3)輸入時(shí)先輸入到一個(gè)對(duì)象中然后賦值給數(shù)組 (4)注意要用一個(gè)index來記錄該輸入第幾個(gè)數(shù)組了 (5)用函數(shù)來進(jìn)行輸入等操作 輸入時(shí)在主函數(shù)里定義一個(gè)成員變量 2、查詢: (1)遍歷數(shù)組再遍歷對(duì)象3、修改學(xué)生信息 (1)4、刪除學(xué)生信息 (1)進(jìn)行覆蓋問題: (1)其他類里的局部變量怎么引入到main里? 是不是可以直接在其他class里給main里的變量賦值?否*****************************************/(2)
import java.util.Scanner;class Student{Student(){}Student (String stuNo, String stuName, String gender, byte age, int score){this.stuNo = stuNo;this.stuName = stuName;this.gender = gender;this.age = age;this.score = score;}String stuNo;String stuName;String gender;byte age;int score; }class StuData{Student stuArray[] = new Student[10];int dataindex = 0;void insertStu(Student stu){if(dataindex == stuArray.length){Student newArray[] = new Student[stuArray.length + (stuArray.length >> 1)];for(int i = 0; i<dataindex; i++){newArray[i] = stuArray[i];}stuArray = newArray;///賦值時(shí)不用加[]}stuArray[dataindex] = stu;dataindex++;}Student checkStuNo(String stuNo){for(int i = 0; i < dataindex; i++){if(stuArray[i].stuNo.equals(stuNo)) ///.equals{return stuArray[i]; }}return null;}void delateStu(String stuNo){///錯(cuò)誤:找不到符號(hào) 有可能是大小寫的問題,只要雙擊不論大小寫只要拼寫相同就變綠boolean flag = false;for(int i = 0; i < dataindex; i++){if (stuArray[i].stuNo.equals(stuNo)){stuArray[i] = stuArray[i + 1];flag = true;}}if(flag){dataindex--;}}void updateStu(Student stu){for(int i = 0; i < dataindex; i++){if(stuArray[i].stuNo.equals(stu.stuNo)){stuArray[i] = stu;}}}void printStu(Student stu){System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");System.out.print("學(xué)生編號(hào) " + stu.stuNo + " ");System.out.print("學(xué)生姓名 " + stu.stuName + " ");System.out.print("學(xué)生性別 " + stu.gender + " ");System.out.print("學(xué)生年齡 " + stu.age + " ");System.out.println("學(xué)生成績(jī) " + stu.score + " ");System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");}void printAllstuInf(){for(int i = 0; i < dataindex; i++){printStu(stuArray[i]);}} }class StuSys{StuData stud = new StuData();void insertStu(Scanner scanner){System.out.println("請(qǐng)輸入學(xué)生的學(xué)號(hào) 姓名 性別 年齡 成績(jī)(輸入出用空格分開):");String stuNo = scanner.next();String stuName = scanner.next();String gender = scanner.next();byte age = scanner.nextByte();int score = scanner.nextInt();int i = 0;if(stud.checkStuNo(stuNo) == null){Student stu = new Student(stuNo, stuName, gender, age, score);stud.insertStu(stu);System.out.println("學(xué)生信息添加成功!!!");}else{System.out.println("您輸入的學(xué)生編號(hào)已存在,請(qǐng)選擇下一步操作");System.out.println("1、繼續(xù)添加 2、退出系統(tǒng)");i = scanner.nextInt();if(i == 1){insertStu(scanner); ///遞歸}else if(i == 2){System.exit(0);}else{System.out.println("您輸入的數(shù)據(jù)不合規(guī)!!!");}}}void delateStu(Scanner scan){int i = 0;///可以重復(fù)使用i嗎System.out.println("請(qǐng)輸入要?jiǎng)h除學(xué)生的編號(hào):");String stuNo = scan.next();if(stud.checkStuNo(stuNo) != null){stud.delateStu(stuNo);System.out.println("學(xué)成信息刪除成功!!!");}else {System.out.println("您輸入的學(xué)號(hào)不存在,請(qǐng)選擇下一步操作");System.out.println("1、繼續(xù)刪除 2、退出系統(tǒng)");if(i == 1){delateStu(scan); }else if(i == 2) {System.exit(0);}else{System.out.println("您輸入的數(shù)據(jù)不合規(guī)!!!");}}}void updateStu(Scanner scanner){int i = 0;System.out.println("請(qǐng)輸入要修改的學(xué)生的編號(hào):");String stuNo = scanner.next();Student stu = new Student();///非要等與null嗎??調(diào)試后的總結(jié):1不能直接= new student 因?yàn)闃?gòu)造方法里沒有student() (無參數(shù)的構(gòu)造方法)///解決方法:加一個(gè)無參的構(gòu)造方法if(stud.checkStuNo(stuNo) != null){System.out.println("請(qǐng)輸入學(xué)生的姓名 性別 年齡 成績(jī) (輸入時(shí)用空格分開)");stu.stuNo = stuNo;stu.stuName = scanner.next();stu.gender = scanner.next();stu.age = scanner.nextByte();stu.score = scanner.nextInt();stud.updateStu(stu);System.out.println("學(xué)生信息修改成功!!!");}else{System.out.println("您輸入的學(xué)生編號(hào)不存在,請(qǐng)選擇下一步操作");System.out.println("1、繼續(xù)修改 2、退出系統(tǒng)");i = scanner.nextInt();if(i == 1){insertStu(scanner); }else if(i == 2){System.exit(0);}else{System.out.println("您輸入的數(shù)據(jù)不合規(guī)!!!");}}}void showStuInfo(Scanner scanner){int i = 0;Student stu = new Student();System.out.println("請(qǐng)輸入查看學(xué)生的編號(hào)");String stuNo = scanner.next();if((stu = stud.checkStuNo(stuNo)) != null){stud.printStu(stu);}else{System.out.println("您輸入的學(xué)號(hào)不存在,請(qǐng)選擇下一步操作");System.out.println("1、繼續(xù)查看 2、退出系統(tǒng)");i = scanner.nextInt();if(i == 1){showStuInfo(scanner); }else if(i == 2){System.exit(0);}else{System.out.println("您輸入的數(shù)據(jù)不合規(guī)!!!");}}}void showAllStuInfo(){stud.printAllstuInf();} }class Main{public static void main(String[] strs){System.out.println("歡迎使用tyrantfor的學(xué)生管理系統(tǒng)");Scanner scan = new Scanner(System.in);Main main = new Main();StuSys s1 = new StuSys();while(true){main.showMenu();switch(scan.nextInt()){case 0:System.exit(0);case 1:s1.insertStu(scan);break;case 2:s1.delateStu(scan);break;case 3:s1.updateStu(scan);break;case 4:s1.showStuInfo(scan);break;case 5:s1.showAllStuInfo();break;default:System.out.println("輸入數(shù)據(jù)不合規(guī)!!!");}}}void showMenu(){System.out.println("請(qǐng)選擇操作:\n\n");System.out.println("1:增加學(xué)生信息");System.out.println("2:刪除學(xué)生信息");System.out.println("3:修改學(xué)生信息");System.out.println("4:根據(jù)學(xué)號(hào)查看學(xué)生信息");System.out.println("5:查看所有學(xué)生信息");System.out.println("0:退出系統(tǒng)");} }總結(jié)
以上是生活随笔為你收集整理的java学生管理系统的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python数据分析Numpy库方法简介
- 下一篇: [react] 为什么标签里的for要写