random用float求80-100以内随机值

编程模拟10位评委给选手打分,随机打分,去掉一个最大值和最小值,剩下的8个求平均值,即为选手的最终成绩,结果保留两位小数

import java.util.Scanner;
public class Test07 {
    public static void main(String[] args) {

        Random r = new Random();
        float[] score = new float[10];
        float sum = 0;

        // 遍历数组
        for (int i = 0; i < score.length; i++) {
            score[i] = 80+r.nextFloat(21);  //随机生成80-100的数
            System.out.print(score[i] + "\t");  // 打印生成的分数
        }

        float max = score[0];
        float min = score[0];

        // 找出最大值max和最小值min
        for (int i = 0; i < score.length; i++) {
            sum += score[i];
            if(score[i] > max){
                max = score[i];
            }else if(score[i] < min){
                min = score[i];
            }
        }
        

        float avg = (sum-max-min)/(score.length-2);
        System.out.println(String.format("%.2f", avg));
    }
}

 

https://blog.csdn.net/weixin_51311218/article/details/109585564

// 生成 80-100 的分数
Random random = new Random();
arr[i] = random.nextInt(21) + 80
// 排序
Arrays.sort(arr);
// 只取下标为 1 - 8 的值求和再除以 8 就好了

 

  public static void main(String[] args) {
        double[] score = new double[10];
        double sum = 0;

        // 遍历数组
        for (int i = 0; i < score.length; i++) {
            score[i] = 80+Math.random()*20;  //随机生成80-100的数
             //保留两位小数,如果不需要可以直接输出score[i]
             System.out.println("第"+(i+1)+"评委打分:"+String.format("%.2f", score[i]));
        }
        double max = score[0];
        double min = score[0];
        // 找出最大值max和最小值min
        for (int i = 0; i < score.length; i++) {
            sum += score[i];
            if(score[i] > max){
                max = score[i];
            }else if(score[i] < min){
                min = score[i];
            }
        }
        double avg = (sum-max-min)/(score.length-2);
        System.out.println("选手最终成绩:"+String.format("%.2f", avg));
    }

 

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632