Java数组和字符串:练习-竞赛打分

【课堂练习】
在某竞赛中,有10个评委为参赛的选手打分,分数为0~100分。选手最后得分为:去掉一个最高分和一个最低分后其余8个分数的平均值。请编写一个程序计算并打印输出选手最后得分。

运行代码:

import java.util.Scanner;

public class JingSai {
    
    

	public static void main(String[] args) {
    
    
		//1. 声明数组
		double[] scores;
		
		//2. 创建数组对象
		scores = new double[10];
		System.out.println("请输入10个百分制成绩:");
		//3. 数组的初始化,使用输入值循环初始化
		Scanner sc = new Scanner(System.in);
		
		for (int i = 0; i < scores.length; i++) {
    
    
			scores[i] = sc.nextDouble();
		}
		
		//4. 处理问题,最大值、最小值、平局分(求和)
		double max = scores[0]; //假设数组第一个元素的值最大
		double min = scores[0]; //假设数组第一个元素的值最小
		double sum = 0;
		
		for (int i = 0; i < scores.length; i++) {
    
    
			sum += scores[i];
			if (scores[i] > max) {
    
    //找到更大的值
				max = scores[i];  //保存较大值
			}
			
			if (scores[i] < min) {
    
    //找到更小的值
				min = scores[i];  //保存较小值
			}
		}
		
		double avg = ( sum - max - min )/(scores.length - 2);
		System.out.println("最高分:"+max+",最低分:"+min+",最终得分:"+avg);
		
		sc.close();
	}

}

猜你喜欢

转载自blog.csdn.net/m0_46700215/article/details/106458933