Squirrel_zh 2022-02-20 00:24 采纳率: 0%
浏览 49

在eclipse中遇到求和,累加的sum最后总比正确结果少 1 ,能有人帮我解决这个问题吗?

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图

import java.util.Scanner;

public class text1 {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int [] scores  = new int [3];
    Scanner scanner = new Scanner(System.in);
    
    for(int i=0;i<scores.length;i++) {
        System.out.print("请输入第"+(i+1)+"个评委的打分:");
        scores[i] = scanner.nextInt();
    }
    scanner.close();

    int temp,sum = 0;
    for(int i = 0 ; i<scores.length; i++) {            
        sum += scores[i];              
        /*
        在 sum 求和的过程中  数据总是比正确答案少 1
        */
        for (int j = 0; j < scores.length-i-1; j++) {
            if(scores[j] < scores[j+1]) {
                temp = scores[j];
                scores[j] = scores[j+1];
                scores[j+1] = temp;
            }
        }
        
}
    System.out.println("总分是:"+sum);
    double result = (sum - scores[0] - scores[scores.length-1])*1.0 / (scores.length-2); 
    System.out.println("最高分:"+scores[0]);
    System.out.println("最低分:"+scores[scores.length-1]);
    System.out.println("平均分(去掉最高分、最低分):"+result);
}

}

运行结果及报错内容

请输入第1个评委的打分:2
请输入第2个评委的打分:3
请输入第3个评委的打分:4
总分数:8
最高分:4
最低分:2
平均分(去掉最高分、最低分):2.0

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

2条回答 默认 最新

  • piaoyiren 2022-02-20 08:01
    关注

    你的程序是这样错的,你看我的

    ```java
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    int [] scores = new int [3];
    Scanner scanner = new Scanner(System.in);

        for(int i=0;i<scores.length;i++) {
            System.out.print("请输入第"+(i+1)+"个评委的打分:");
            scores[i] = scanner.nextInt();
        }
        scanner.close();
     
        int temp,sum = 0;
        for(int i = 0 ; i<scores.length; i++) {            
                        
            /*
            在 sum 求和的过程中  数据总是比正确答案少 1
            */
            for (int j = 0; j < scores.length-i-1; j++) {
                if(scores[j] < scores[j+1]) {
                    temp = scores[j];
                    scores[j] = scores[j+1];
                    scores[j+1] = temp;
                }
            }
            sum += scores[i];  
            
    }
        System.out.println("总分是:"+sum);
        double result = (sum - scores[0] - scores[scores.length-1])*1.0 / (scores.length-2); 
        System.out.println("最高分:"+scores[0]);
        System.out.println("最低分:"+scores[scores.length-1]);
        System.out.println("平均分(去掉最高分、最低分):"+result);
    }
    

    ```程序

    评论

报告相同问题?

问题事件

  • 创建了问题 2月20日

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表