在数组旁边显示平均值

我是 Java 新手,所以很抱歉。


我有两个数组:


int[] grades = {64,55,45,67,65,88};

String[] unitCode = {"APP:","BSAD:","CF","DAD:","N&CS:","POP:"};

我需要将每一个都放在一起形成:


[APP:64, BSAD:55, CF45, DAD:67, N&CS:65, POP:88]

我已经成功地用这段代码实现了这一点:


    public String[] unitMarks(int[] grades, String[] unitCode) 

    {

        double sum = 0;

        for (double i : grades)

        sum += i;

        double average = (sum/grades.length);


            for (int i = 0; i <grades.length; i++)

            {

                String combination = unitCode[i]+grades[i];

                unitCode[i] = combination;


        }


            return unitCode;

    }

但我还需要在最后显示平均成绩,所以应该说:


[APP:64, BSAD:55, CF:45, DAD:67, N&CS:65, POP:88, Average:64.0]

我已经编写了用于查找成绩数组平均值的代码。我只是在使用组合数组返回平均值时遇到问题(并且还在其之前显示“平均值:”)。


我尝试过做类似的事情 -


String includedAverage = unitCode+" Average:"+average;

return includedAverage;

但随后它开始说它无法从 String 转换为 String[]。如果我将方法返回类型更改为 String 它不起作用:


System.out.println(Arrays.toString(myArrays.unitMarks(grades, unitCode)));

任何帮助或指示都会很棒。谢谢。


眼眸繁星
浏览 144回答 3
3回答

www说

您可以添加一个比数组多一个空间的结果数组unitcode,并将值分配给它,而不是修改输入:public String[] unitMarks(int[] grades, String[] unitCode) {&nbsp; &nbsp; double sum = 0;&nbsp; &nbsp; for (double i : grades) {&nbsp; &nbsp; &nbsp; &nbsp; sum += i;&nbsp; &nbsp; }&nbsp; &nbsp; double average = (sum / grades.length);&nbsp; &nbsp; String[] result = new String[grades.length +1];&nbsp; &nbsp; for (int i = 0; i < grades.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; String combination = unitCode[i] + grades[i];&nbsp; &nbsp; &nbsp; &nbsp; result[i] = combination;&nbsp; &nbsp; }&nbsp; &nbsp; result[result.length-1]= "Average:"+average;&nbsp; &nbsp; return result;}

HUWWW

有两种方法可以做到这一点。1. 使用ArrayList,当向其中添加元素时,它的大小会增加public List<String> unitMarks(int[] grades, String[] unitCode)&nbsp;{&nbsp; &nbsp; List<String> unitCodeArrayList = new ArrayList<String>();&nbsp; &nbsp; double sum = 0;&nbsp; &nbsp; for (double i : grades)&nbsp; &nbsp; sum += i;&nbsp; &nbsp; double average = (sum/grades.length);&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i <grades.length; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String combination = unitCode[i]+grades[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unitCodeArrayList.add(combination);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; unitCodeArrayList.add("Average:"+average);&nbsp; &nbsp; &nbsp; &nbsp; return unitCodeArrayList;}使用原始数组,但以这种方式附加到它(因为我不确定它的大小是多少)public String[] unitMarks(int[] grades, String[] unitCode)&nbsp;{double sum = 0;for (double i : grades)sum += i;double average = (sum/grades.length);for (int i = 0; i <grades.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; String combination = unitCode[i]+grades[i];&nbsp; &nbsp; &nbsp; &nbsp; unitCode[i] = combination;}&nbsp; &nbsp; String[] resUnitCode = appendArray(unitCode, "Average:"+average);&nbsp; &nbsp; return resUnitCode;}private String[] appendArray(String[] array, String x){String[] result = new String[array.length + 1];for(int i = 0; i < array.length; i++)&nbsp; &nbsp; result[i] = array[i];result[result.length - 1] = x;return result;}

喵喔喔

如果只需要显示数值,可以使用 Stream 计算平均值并一次性显示:private void displayGradesAndAverage(int[] grades, String[] unitCode) {&nbsp; &nbsp; IntStream.range(0, grades.length)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .peek(i -> System.out.print(unitCode[i] + grades[i] + ", "))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(i -> grades[i])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .average()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ifPresent(avg -> System.out.println("Average:" + avg));}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java