Java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)
本文實(shí)例為大家分享了Java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
新手練手小項(xiàng)目,主要練習(xí)面向?qū)ο?、循環(huán)、數(shù)組、排序等等。
本人也是自學(xué)練手,代碼有不正確的或不完善的也請(qǐng)多多包涵,留言指導(dǎo)。
感謝!
一、創(chuàng)建學(xué)生類
儲(chǔ)存學(xué)生的基本信息
public class Student { private int no; private String name; Result result; public Student() { super(); } public Student(int no, String name,Result result) { super(); this.result = result; this.name = name; this.no = no; } public Result getResult() { return result; } public void setResult(Result result) { this.result = result; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getNo() { return no; } public void setNo(int no) { this.no = no; } public String toString(){ return "\t" + no + "\t" + name + "\t" + result; } }
二、創(chuàng)建成績(jī)類
便于成績(jī)的增加,易于管理
public class Result {//成績(jī)類 private int java; private int python; private int c_shar; private int sql; private int sum; private int avg; public Result() { } public Result(int java, int python, int c_shar, int sql) { super(); this.java = java; this.python = python; this.c_shar = c_shar; this.sql = sql; } public int getJava() { return java; } public void setJava(int java) { this.java = java; } public int getPython() { return python; } public void setPython(int python) { this.python = python; } public int getC_shar() { return c_shar; } public void setC_shar(int c_shar) { this.c_shar = c_shar; } public int getSql() { return sql; } public void setSql(int sql) { this.sql = sql; } public int getSum(){ return this.java + this.python + this.c_shar + this.sql; } public int getAvg(){ return this.getSum() / 4; } public String toString(){ return java + "\t" + python + "\t" + c_shar + "\t" + sql + "\t" + this.getSum() + "\t" + this.getAvg(); } }
三、創(chuàng)建學(xué)生管理類
主要功能在此類中,例如添加學(xué)生信息、查找學(xué)生等等
import java.util.Scanner; public class StudentAdmin {//管理學(xué)生類 Scanner in = new Scanner(System.in); /** * * @param name 傳一個(gè)name添加學(xué)生姓名 * @param Arr傳一個(gè)學(xué)生數(shù)組,將學(xué)生對(duì)象保存 * @throws AddStudentException */ public void addStudent(String name,Student[] Arr) throws AddStudentException{//添加學(xué)生方法 AddStudentException ase = new AddStudentException("學(xué)生已達(dá)上限,添加失??!"); Student stu = new Student(); Result r = new Result(); stu.setNo(setIndex(Arr) + 1); stu.setName(name); stu.setResult(r); if(setIndex(Arr) == -1){ throw ase; }else{ Arr[this.setIndex(Arr)] = stu; } } /** * * @param Arr傳一個(gè)數(shù)組 * @return返回空數(shù)組元素的下標(biāo) */ public int setIndex(Student[] Arr){ for(int i = 0; i < Arr.length; i++){ if(null == Arr[i]){ return i; } } return -1; } public void print(Student[] arr){//打印學(xué)生對(duì)象 for(int i = 0; i < arr.length; i++){ if(judge(i+1,arr[i])){ System.out.println(arr[i]); } } } public boolean judge(int no,Student stu){//判斷數(shù)組元素編號(hào)和傳的編號(hào)是否一致 if(stu != null){ if(stu.getNo() == no){ return true; }else{ return false; } } return false; } public void search(String name,Student[] arr){//查找學(xué)生方法 for(int i = 0; i < arr.length; i++){ if(judge(i + 1,arr[i])){ if(arr[i].getName().equals(name)){ System.out.println(arr[i]); return; } } } System.out.println("沒(méi)有找到此學(xué)生,請(qǐng)確認(rèn)輸入的姓名是否正確!"); } public void update(int no,String name,Student[] arr){//更新學(xué)生姓名 for(int i = 0; i < arr.length; i++){ if(judge(i + 1,arr[i])){ arr[i].setName(name); System.out.println(arr[i]); return; } } System.out.println("沒(méi)有此編號(hào),請(qǐng)確認(rèn)輸入的編號(hào)是否正確!"); } public void del(int no,Student[] arr){//刪除學(xué)生信息 for(int i = 0; i < arr.length; i++){ if(judge(no,arr[i])){ arr[i] = null; return; } } System.out.println("沒(méi)有此編號(hào),請(qǐng)確認(rèn)輸入的編號(hào)是否正確!"); } public void stuResult(int no,Student[] arr){//根據(jù)編號(hào)輸入學(xué)生各科成績(jī) for(int i = 0; i < arr.length; i++){ if(judge(i + 1,arr[i])){ if(arr[i].getNo() == no){ System.out.println("請(qǐng)輸入" + arr[i].getName() + "的java成績(jī):"); arr[i].result.setJava(in.nextInt()); System.out.println("請(qǐng)輸入" + arr[i].getName() + "的python成績(jī):"); arr[i].result.setPython(in.nextInt()); System.out.println("請(qǐng)輸入" + arr[i].getName() + "的c_char成績(jī):"); arr[i].result.setC_shar(in.nextInt()); System.out.println("請(qǐng)輸入" + arr[i].getName() + "的sql成績(jī):"); arr[i].result.setSql(in.nextInt()); break; } } } } public void paiXu(int no,Student[] arr){//根據(jù)某科成績(jī)排序 switch(no){ case 1: for(int i = 0; i < arr.length-1; i++){ for(int j = i + 1; j < arr.length; j++){ if(arr[j].result.getJava() > arr[i].result.getJava()){ Student max = arr[i]; arr[i] = arr[j]; arr[j] = max; } } } for(int i = 0; i < arr.length; i++){ System.out.println(arr[i]); } break; case 2: for(int i = 0; i < arr.length-1; i++){ for(int j = i + 1; j < arr.length; j++){ if(arr[j].result.getPython() > arr[i].result.getPython()){ Student max = arr[i]; arr[i] = arr[j]; arr[j] = max; } } } for(int i = 0; i < arr.length; i++){ System.out.println(arr[i]); } break; case 3: for(int i = 0; i < arr.length-1; i++){ for(int j = i + 1; j < arr.length; j++){ if(arr[j].result.getC_shar() > arr[i].result.getC_shar()){ Student max = arr[i]; arr[i] = arr[j]; arr[j] = max; } } } for(int i = 0; i < arr.length; i++){ System.out.println(arr[i]); } break; case 4: for(int i = 0; i < arr.length-1; i++){ for(int j = i + 1; j < arr.length; j++){ if(arr[j].result.getSql() > arr[i].result.getSql()){ Student max = arr[i]; arr[i] = arr[j]; arr[j] = max; } } } for(int i = 0; i < arr.length; i++){ System.out.println(arr[i]); } break; default: System.out.println("你輸入的編號(hào)錯(cuò)誤!"); } } public void sumPaiXu(Student[] arr){//根據(jù)總分進(jìn)行排序 for(int i = 0; i < arr.length-1; i++){ for(int j = i + 1; j < arr.length; j++){ if(arr[j].result.getSum() > arr[i].result.getSum()){ Student max = arr[i]; arr[i] = arr[j]; arr[j] = max; } } } for(int i = 0; i < arr.length; i++){ System.out.println(arr[i]); } return; } }
四、異常類
練習(xí)自定義異常類的創(chuàng)建及使用
public class AddStudentException extends Exception{ public AddStudentException(){} public AddStudentException(String s){ super(s); } }
五、測(cè)試類(主程序)
import java.util.Scanner; public class Test { public static void main(String[] args){ Scanner in = new Scanner(System.in); StudentAdmin admin = new StudentAdmin(); System.out.println("請(qǐng)定義學(xué)生的人數(shù):"); Student[] stuArr = new Student[in.nextInt()]; while(true){ System.out.println("請(qǐng)選擇你要執(zhí)行的功能:"); System.out.println("1:添加一個(gè)學(xué)生"); System.out.println("2:查找一個(gè)學(xué)生"); System.out.println("3:根據(jù)學(xué)生編號(hào)更新 學(xué)生基本信息"); System.out.println("4:根據(jù)學(xué)生編號(hào)刪除學(xué)生"); System.out.println("5:根據(jù)編號(hào)輸入學(xué)生各科成績(jī)"); System.out.println("6:根據(jù)某科成績(jī)進(jìn)行排序"); System.out.println("7:根據(jù)總分進(jìn)行排序"); System.out.println("0:退出系統(tǒng)"); int num = in.nextInt(); switch(num){ case 0 : System.exit(0); case 1 : System.out.println("請(qǐng)輸入學(xué)生姓名:"); try { admin.addStudent(in.next(), stuArr); } catch (AddStudentException e) { e.printStackTrace(); } admin.print(stuArr); break; case 2 : System.out.println("請(qǐng)輸入要查找的學(xué)生姓名:"); String name = in.next(); admin.search(name, stuArr); break; case 3 : System.out.println("請(qǐng)輸入要修改的編號(hào):"); int no = in.nextInt(); System.out.println("請(qǐng)重新輸入學(xué)生姓名:"); String name1 = in.next(); admin.update(no, name1, stuArr); break; case 4 : System.out.println("請(qǐng)輸入學(xué)生編號(hào):"); int no1 = in.nextInt(); admin.del(no1, stuArr); System.out.println("刪除學(xué)生信息成功!"); break; case 5 : System.out.println("請(qǐng)輸入學(xué)生編號(hào):"); int no2 = in.nextInt(); admin.stuResult(no2, stuArr); admin.print(stuArr); break; case 6 : System.out.println("請(qǐng)輸入編號(hào)對(duì)應(yīng)成績(jī)進(jìn)行排序【1:java 2:python 3:c_char 4:sql】"); int no3 = in.nextInt(); admin.paiXu(no3, stuArr); break; case 7 : admin.sumPaiXu(stuArr); break; default : System.out.println("您輸入的編號(hào)有誤,請(qǐng)重新輸入!"); break; } } } }
總結(jié)
本人也是剛學(xué)習(xí)java不久,一直在看視頻自學(xué)。目前學(xué)習(xí)到了集合(剛開(kāi)始),線程、IO還沒(méi)學(xué)。這個(gè)小的項(xiàng)目也是我自己在練手,鞏固學(xué)習(xí)的基礎(chǔ)知識(shí),只用到了最基本的知識(shí)。
在做這個(gè)項(xiàng)目的時(shí)候有很多不懂的地方。
例如:
1、Student類中的Result result這個(gè)實(shí)例變量的傳參,我發(fā)現(xiàn)如果把Result result這個(gè)屬性私有化無(wú)法賦值,最后便把訪問(wèn)權(quán)限改成了缺省
2、“根據(jù)編號(hào)刪除學(xué)生信息”不知道怎么刪除數(shù)組中的學(xué)生對(duì)象,現(xiàn)在知道原來(lái)把數(shù)組賦值為“null”就行了
等等
通過(guò)這個(gè)小練習(xí),也學(xué)習(xí)到了很多很多。代碼還有很多不完善的地方,分享給大家,請(qǐng)大家指導(dǎo)!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Maven學(xué)習(xí)教程之搭建多模塊企業(yè)級(jí)項(xiàng)目
本篇文章主要介紹了Maven學(xué)習(xí)教程之搭建多模塊企業(yè)級(jí)項(xiàng)目 ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10Spring Boot中的@ConfigurationProperties注解解讀
在SpringBoot框架中,@ConfigurationProperties注解是處理外部配置的強(qiáng)大工具,它允許開(kāi)發(fā)者將配置文件中的屬性自動(dòng)映射到Java類的字段上,實(shí)現(xiàn)配置的集中管理和類型安全,通過(guò)定義配置類并指定前綴,可以將配置文件中的屬性綁定到Java對(duì)象2024-10-10