我有2个阵列,String[][] names
和int[][] grades
,存储我想在更长的代码,以打印为表一类的名称和等级。我能够用我在main中调用的方法计算每行的平均值。我很难弄清楚如何做每列的平均值。任何建议,将不胜感激。该行平均我写了一个方法:二维数组列一般的Java
//determine average for each student
public double rowAverage(int[] rowOfGrades) {
int total = 0;
//sum grades for each student
for(int grade : rowOfGrades){
total += grade;
}
//return average of student grades
return (double) total/rowOfGrades.length;
}//end getAverage
,然后印在我的主与
//creates rows and columns of text for array names and grades
for(int student=0; student<names.length; student++) {
System.out.printf("%s",names[student]); //student name
for(int test : grades[student]) {
System.out.printf("\t%7d",test); //test grades
}
//call method getAverage to calculate students grade average
//pass row of grades as the argument to getAverage
double average = rowAverage(grades[student]);
System.out.printf("%12.2f", average);
}