如何在数组中高于平均水平的数字旁边显示星号?

我正在尝试对数字列表进行排序,使其显示如下(高于平均数字旁边有一个星号):


53

56

68

78*

79*

80*

89*

90*

100*

我已经尝试过这个(分数是我的数字数组):


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

    if (scores[i] > average) {

        System.out.println(scores[i] + "*");

    }

}

和这个:


for (double element : scores) {

    int aboveAvg = 0;

    for (int i = 0, i <scores.length; i++){

        if (scores[i] > average) {

            aboveAvg++;

            System.out.println(element + "*");

        }

    }

}


吃鸡游戏
浏览 95回答 1
1回答

慕村225694

您可以按如下方式进行操作:import java.util.ArrayList;import java.util.Collections;import java.util.List;public class Main {&nbsp; &nbsp; public static void main(String[] pArgs) {&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> numList = new ArrayList<Integer>();&nbsp; &nbsp; &nbsp; &nbsp; numList.add(53);&nbsp; &nbsp; &nbsp; &nbsp; numList.add(100);&nbsp; &nbsp; &nbsp; &nbsp; numList.add(56);&nbsp; &nbsp; &nbsp; &nbsp; numList.add(90);&nbsp; &nbsp; &nbsp; &nbsp; numList.add(68);&nbsp; &nbsp; &nbsp; &nbsp; numList.add(89);&nbsp; &nbsp; &nbsp; &nbsp; numList.add(78);&nbsp; &nbsp; &nbsp; &nbsp; numList.add(89);&nbsp; &nbsp; &nbsp; &nbsp; numList.add(80);&nbsp; &nbsp; &nbsp; &nbsp; int sum = 0, average = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (Integer n : numList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += n;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; average = sum / numList.size();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Average: " + average);&nbsp; &nbsp; &nbsp; &nbsp; Collections.sort(numList);&nbsp; &nbsp; &nbsp; &nbsp; for (Integer n : numList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (n > average) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(n + "*");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(n);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}输出:Average: 785356687880*89*89*90*100*
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java