本帖最后由 倒戈卸甲 于 2020-4-16 23:46 编辑
给你写个规范答案,希望你能悟出点什么吧。public class student {
public static void main(String[] args) {
int[][] scoreTable= {
{80,75,78,93},
{67,87,98,65},
{86,72,60,76},
{76,80,76,63},
{82,70,90,67}
};
int stuCount=scoreTable.length;
int projCount=scoreTable[0].length;
System.out.println("=========第一题==========");
demo1(scoreTable,stuCount,projCount);
System.out.println("=========第二题==========");
demo2(scoreTable,stuCount,projCount);
System.out.println("=========第三题==========");
demo3(scoreTable,stuCount,projCount);
}
/*
* demo1
* 计算并输出每个学生的总成绩
*/
private static void demo1(int[][] scoreTable,int stuCount,int projCount) {
for (int i = 0; i < stuCount; i++) {
int sum=0;
for (int j = 0; j < projCount; j++) {
sum +=scoreTable[i][j];
}
System.out.println(i+"号学生的成绩为:"+sum);
}
}
/*
* demo2
* 打印每科成绩最高分,并输出所在行号
*/
private static void demo2(int[][] scoreTable,int stuCount,int projCount) {
for (int i = 0; i < projCount; i++) {
//这里将四科成绩的每一科成绩单独存入一个数组
int[] projectArr=new int[stuCount];
for (int j = 0; j < stuCount; j++) {
projectArr[j]=scoreTable[j][i];
}
System.out.print("第"+(i+1)+"科成绩:\t");
findMax(projectArr,stuCount);
}
}
//这里额外定义一个寻找最大值的函数,由第二问和第三问共同使用
private static void findMax(int[]arr,int stuCount) {
int station=0;
int maxScore=0;
for (int i = 0; i < stuCount; i++) {
if (arr[i]>maxScore) {
maxScore=arr[i];
station=i;
}
}
System.out.println("最高分是"+maxScore+",由"+station+"号同学获得");
}
/*
* demo3
* 打印每科成绩最高分,并输出所在行号
*/
private static void demo3(int[][] scoreTable,int stuCount,int projCount) {
//这里将五个人的总成绩存入一个数组
int[] studentArr=new int[stuCount];
for (int i = 0; i < stuCount; i++) {
int sum=0;
for (int j = 0; j < projCount; j++) {
sum +=scoreTable[i][j];
}
studentArr[i]=sum;
}
System.out.print("总成绩:\t");
findMax(studentArr,stuCount);
}
}
|