最好成绩数组

所以我必须这样做“打印测试分数,找到班级的总体平均水平,以及哪一行的平均水平最好。” 我很困惑


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


有只小跳蛙
浏览 162回答 2
2回答

眼眸繁星

要找到哪个子数组的平均值更大,您必须计算它们并保持 max :public static void main(String[] args){&nbsp; &nbsp; int array1[][] = {{90, 80, 65, 100}, {55, 94, 86}, {82}, {77, 100}};&nbsp; &nbsp; double maxAverage = Double.MIN_VALUE;&nbsp; &nbsp; for(int[] sub : array1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//iterate over sub-arrays&nbsp; &nbsp; &nbsp; &nbsp; double average = averageOfArray(sub);&nbsp; &nbsp; &nbsp; //compute its average&nbsp; &nbsp; &nbsp; &nbsp; maxAverage = Math.max(maxAverage, average);//get the max of it and the previous max&nbsp; &nbsp; }&nbsp; &nbsp; System.out.printn(maxAverage)}static double averageOfArray(int[] array){&nbsp; &nbsp; double sum = 0;&nbsp; &nbsp; for(int i=0; i<array.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; sum += array[i];&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; 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)

一只名叫tom的猫

当您需要对数组进行字符串化时,您可以使用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]]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java