java編寫學生成績錄入系統
阿新 • • 發佈:2019-02-14
1.學生類 包括學生的姓名和各科成績
public class Score { public String name; public double EnglishGrade, MathGrade,phyicalGrade, chemicalGrade,biologicalGrade; Score() { } public Score(String name,double EnglishGrade, double MathGrade,double phyicalGrade, double chemicalGrade,double biologicalGrade){ this.name = name; this.EnglishGrade = EnglishGrade; this.MathGrade = MathGrade; this.phyicalGrade = phyicalGrade; this.chemicalGrade = chemicalGrade; this.biologicalGrade = biologicalGrade; } }
2.對學生類進行設定 包括設定查詢等
package score; public class ScoreZip { Score[] S = new Score[5]; public void setData(Score name, int index) {//新增成績類到成績陣列中 S[index] = name; } public Score[] getData() { //返回學生陣列 return S; } public Score SerchData(String name) { //查詢學生各科成績 for (int index = 0; index < S.length; index++) { if (name !=null && S[index] != null ) if (S[index].name.equals(name)){ return S[index]; } } return null; } }
3.學生成績錄入及學生成績清單
package score; import java.util.Scanner; public class ScoreZip2 { public static void main(String[] arr) { Scanner Sc = new Scanner(System.in); ScoreZip Sr = new ScoreZip(); for (int i = 0; i < 5; i++) { //這裡定義長度為五的學生成績表 System.out.println("輸入學生姓名:"); String name = Sc.next(); if (name.equals("n")) { break; } System.out.println("英語分數:"); double English = Sc.nextDouble(); System.out.println("數學分數:"); double Math = Sc.nextDouble(); System.out.println("物理分數:"); double Physical = Sc.nextDouble(); System.out.println("化學分數:"); double Chemical = Sc.nextDouble(); System.out.println("生物分數:"); double Biology = Sc.nextDouble(); Score s = new Score(name, English,Math,Physical,Chemical,Biology); Sr.setData(s, i); } System.out.println("=========================================" + "學生成績表======================================"); System.out.println("姓名\t\t英語成績\t\t數學成績\t\t物理成績\t\t化學成績\t\t生物成績"); Score[] b = Sr.getData(); for (Score S : b) { if (S == null) { break; } System.out.println(S.name + "\t\t" + S.EnglishGrade+"\t\t"+S.MathGrade +"\t\t"+S.phyicalGrade+"\t\t"+S.chemicalGrade+"\t\t"+S.biologicalGrade); } System.out.println("是否查詢學生成績? 是Y 否N"); String flag = Sc.next(); if(flag.equals("Y")){ System.out.println("輸入要查詢的學生名:"); String name = Sc.next(); Score S = Sr.SerchData(name); if (S == null) { System.out.println("對不起,沒有此學生名"); } else { double mg = S.MathGrade; double eg = S.EnglishGrade; double pg = S.phyicalGrade; double cg= S.chemicalGrade; double bg = S.biologicalGrade; double submit = mg+eg+pg+cg+bg; System.out.println("要查詢的學生為:" + name + "\n數學成績為:" + mg + "\n英語成績為:" + eg+ "\n物理成績為:" + pg+ "\n化學成績為:" + cg + "\n生物成績為:" + bg+ "\n總成績成績為:" + submit); } } } }
<span style="font-size:18px;">結果圖:</span>