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