用jaⅴa设计程序输入班级人数和各同学的成绩,设计三种方法分别得出最小值、最大值和平均值

img

用一维数组来存学生的成绩。
求最大值、最小值、平均分的方法,以一维数组作为参数。


    public static void main(String[] args) {
        System.out.print("输入班级人数:");

        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int i = 1;
        List<Integer> integers = new ArrayList<>();

        while (i <= n) {
            System.out.print("输入第" + i + "位同学成绩:");
            int score = input.nextInt();
            integers.add(score);
            i++;
        }
        System.out.println("最高分是----" + maxScore(integers));
        System.out.println("最低分是----" + minScore(integers));
        System.out.println("平均分是----" + avgScore(integers));
        System.out.println("从大到小----" + sorted(integers));


    }

    public static int maxScore(List<Integer> score) {
        Optional<Integer> max = score.stream().max(Comparator.comparing(Function.identity()));
        return max.get();
    }

    public static int minScore(List<Integer> score) {
        Optional<Integer> min = score.stream().min(Comparator.comparing(Function.identity()));
        return min.get();
    }

    public static Integer avgScore(List<Integer> score) {
        OptionalDouble average = score.stream().mapToInt(a -> a).average();
        int i = new Double(average.getAsDouble()).intValue();
        return i;
    }

    public static List<Integer> sorted(List<Integer> score) {
        Collections.sort(score);
        Collections.reverse(score);
        return score;
    }