栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

评委打分(JAVA代码)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

评委打分(JAVA代码)

需求

在唱歌比赛中,有6名评委给选手打分,范围是【0-100】之间的整数。选手的最后得分为:去掉最高分,最低分之后的4个评委的平均分。

分析

1.把6个评委的分数录入到程序中去(使用数组);

int[] scores = new int[6]

2.遍历数组中每个数据,进行累加求和,并找出最高分,最低分;
3.按照分数的计算规则算出平均分。

正文

1.定义一个动态初始化的数组,用于后期录入6个评委的分数

int[] scores = new int[6];

2.录入6个评委的分数(需要导入扫描器Scanner的包)

		Scanner sc = new Scanner(System.in);
        for (int i = 0; i < scores.length; i++) {
            System.out.println("请输入第" + (i + 1) + "位评委的打分:");
            int score = sc.nextInt();
        }

3.把分数存入数组的对应位置处

scores[i] = score;

4.遍历数组中的每一个数据,找出最大值,最小值以及总分

		int max = scores[0];
        int min = scores[0];
        int sum = 0;
        for (int i = 0; i < scores.length; i++) {
            if (scores[i] > max) {
                //替换最大值变量存储的数据
                max = scores[i];
            }
            if (scores[i] < min) {
                //替换最小值变量存储的数据
                min = scores[i];
            }
            //统计总分
            sum += scores[i];
        }

5.最后统计平均分即可

double result = (sum - max - min) * 1.0 / (scores.length - 2);
        System.out.println("选手最终得分是:" + result);
完整代码
import java.util.Scanner;


public class Demo5 {
    public static void main(String[] args) {
        //1.定义一个动态初始化的数组,用于后期录入6个评委的分数
        int[] scores = new int[6];

        //2.录入6个评委的分数
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < scores.length; i++) {
            System.out.println("请输入第" + (i + 1) + "位评委的打分:");
            int score = sc.nextInt();
            //3.把分数存入数组的对应位置处
            scores[i] = score;
        }

        //4.遍历数组中的每一个数据,找出最大值,最小值以及总分
        int max = scores[0];
        int min = scores[0];
        int sum = 0;
        for (int i = 0; i < scores.length; i++) {
            if (scores[i] > max) {
                //替换最大值变量存储的数据
                max = scores[i];
            }
            if (scores[i] < min) {
                //替换最小值变量存储的数据
                min = scores[i];
            }
            //统计总分
            sum += scores[i];
        }
        //5.统计平均分即可
        double result = (sum - max - min) * 1.0 / (scores.length - 2);
        System.out.println("选手最终得分是:" + result);
        System.out.println("最高分是:" + max);
        System.out.println("最低分是:" + min);
    }
}

运行结果

总结

1.定义一个动态初始化的数组用于存储分数数据;
2.定义三个变量用于保存最大值,最小值和总和;
3.遍历数组中的每个元素,依次进行统计;
4.遍历结束后按照规则计算出结果即可。

暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1017488.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号