所以我必须这样做“打印测试分数,找到全班平均成绩,以及哪条成绩最好。”我很困惑
public class TESTAVG {
public static void main(String[]args) {
int array1[][] = {{90, 80, 65, 100}, {55, 94, 86,}, {82}, {77, 100}};
System.out.println(array1);
}
} //[[I@2a139a55
答案 0 :(得分:0)
当需要对数组进行字符串化时,可以使用 Arrays.toString(arr); 。对于多维数组,应使用Arrays.deepToString(arr);。
int array1[][] = {{90, 80, 65, 100}, {55, 94, 86,}, {82}, {77, 100}};
System.out.println(Arrays.deepToString(array1));
[[90,80,65,100],[55,94,86],[82],[77,100]]
答案 1 :(得分:0)
要查找哪个子数组具有更大的平均值,必须计算它们并保持最大值:
public static void main(String[] args){
int array1[][] = {{90, 80, 65, 100}, {55, 94, 86}, {82}, {77, 100}};
double maxAverage = Double.MIN_VALUE;
for(int[] sub : array1){ //iterate over sub-arrays
double average = averageOfArray(sub); //compute its average
maxAverage = Math.max(maxAverage, average);//get the max of it and the previous max
}
System.out.printn(maxAverage)
}
static double averageOfArray(int[] array){
double sum = 0;
for(int i=0; i<array.length; i++){
sum += array[i];
}
return sum/array.length;
}
或作为Java 8升级方式:
int array1[][] = {{90, 80, 65, 100}, {55, 94, 86,}, {82}, {77, 100}};
double maxAverage = Arrays.stream(array1).mapToDouble(sub -> Arrays.stream(sub).average().orElse(0)).max().orElse(0);
System.out.println(maxAverage)