A. 編寫一個java程序來計算學生考試成績的平均分和他們的分數等級。你可以假設以下數據:
publicclassScore{
//記錄學生的姓名
privateString[] name;
//記錄分數
privateint[][] fraction;
//記錄分數等級
privatechar[] grade;
//存儲數據
privatevoidsaveData(){
this.name=newString[]{"Johnson","Aniston","Cooper","Gupta","Blair"};
this.fraction=newint[][]{{85,83,77,91,76},{80,90,95,93,48},{78,81,11,90,73},{92,83,30,69,87},{23,45,96,38,59}};
this.grade=newchar[this.name.length];
}
publicScore(){
//在構造函數中調用saveData存儲數據。
this.saveData();
}
//計算分數等級。傳入一個分數,返回該分數的評分等級。
privatechargetGrade(intfraction){
if(fraction>100){
//100分的考卷分數居然超過了100,肯定和老師有交易,給你個X!
return'X';
}
elseif(fraction>=85){
return'A';
}
elseif(fraction>=75){
return'B';
}
elseif(fraction>=65){
return'C';
}
elseif(fraction>=50){
return'D';
}
return'F';
}
//輸出成績/平均分/評分/班級平均分/班級評分
publicvoidprintScore(){
intaverage=0;//存儲班級的平均分
for(inti=0;i<this.fraction.length;i++){
System.out.print(this.name[i]+" ");//輸入學生的名字( 是輸出製表符,相當於按一下Tab的效果)
inttemp=0;//臨時存儲數據的變數
for(intx=0;x<this.fraction[i].length;x++){
temp+=this.fraction[i][x];
System.out.print(this.fraction[i][x]+" ");
}
temp=temp/this.fraction[i].length;//此時temp的值就是該學生的平均分
this.grade[i]=this.getGrade(temp);//存入平均分
System.out.println("平均分:"+temp+" 評價"+this.grade[i]);//輸出該學生的平均分和評價
average+=temp;
}
average=average/this.name.length;
System.out.println("班級平均分:"+average+" 班級評價"+this.getGrade(average));
}
publicstaticvoidmain(String[]args){
newScore().printScore();
}
}
運行結果:
Johnson 85 83 77 91 76 平均分:82 評價B
Aniston 80 90 95 93 48 平均分:81 評價B
Cooper 78 81 11 90 73 平均分:66 評價C
Gupta 92 83 30 69 87 平均分:72 評價C
Blair 23 45 96 38 59 平均分:52 評價D
班級平均分:70 班級評價C