原题链接:[编程入门]结构体之成绩统计2
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Student2[] stu = new Student2[n];
for (int i = 0; i < stu.length; i++) {
stu[i] = new Student2();
}
for (int i = 0; i < stu.length; i++) {// 输入学生数据
stu[i].setSno(sc.next());
stu[i].setName(sc.next());
stu[i].setScoreA(sc.nextInt());
stu[i].setScoreB(sc.nextInt());
stu[i].setScoreC(sc.nextInt());
}
int[] sum = new int[n];// 成绩数组
int A = 0;// 分别输入三科成绩
int B = 0;
int C = 0;
int max = 0;
int index = 0;// 总分最高的学生的下标
for (int i = 0; i < stu.length; i++) {
// 存储每个学生的总成绩和各科成绩
sum[i] = stu[i].getScoreA()+stu[i].getScoreB()+stu[i].getScoreC();
max = sum[0];
if (max < sum[i]){// 找出总分最高的学生的下标index
max = sum[i];
index = i;
}
A += stu[i].getScoreA();
B += stu[i].getScoreB();
C += stu[i].getScoreC();
}
System.out.println(A/n+" "+B/n+" "+C/n);// 各科的平均成绩
System.out.println(stu[index].getSno()+" "+stu[index].getName()+" "+stu[index].getScoreA()+" "+stu[index].getScoreB()+" "+stu[index].getScoreC());
}
}
class Student2{
private String sno;
private String name;
private int[] Score = new int[3];
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScoreA() {
return Score[0];
}
public int getScoreB() {
return Score[1];
}
public int getScoreC() {
return Score[2];
}
public void setScoreA(int score) {
Score[0] = score;
}
public void setScoreB(int score) {
Score[1] = score;
}
public void setScoreC(int score) {
Score[2] = score;
9 分
1 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复