问答详情
源自:6-6 编程练习

为啥我这个数组输出最大值和总数不对

public class imooc_shuzhu2 {

    

    public static void main(String[] args) {

    int[] shuzhu = new int[] { 61, 23, 4, 74, 13, 148, 20 };

      int max=shuzhu[0];

      int min=shuzhu[0];

      double sum=0;

      double avg=0;

      for(int i=0;i<shuzhu.length;i++){

     if(shuzhu[i]>max)

     sum=shuzhu[i];      

     if(shuzhu[i]<min)

     min=shuzhu[i];    

     sum+=shuzhu[i];

      }

 avg=sum/shuzhu.length;

System.out.println("最小值"+min);

System.out.println("最大值"+max);

System.out.println("平均数"+avg);

    }

}


提问者:慕仰5913639 2018-03-07 16:20

个回答

  • 风之极端
    2018-03-08 11:12:15
    已采纳

    public class Test {
        public static void main(String[] args) {
                int[] shuzhu = new int[] { 61, 23, 4, 74, 13, 148, 20 };
                int max=shuzhu[0];
                int min=shuzhu[0];
                int sum=0;
                double avg=0;
                for(int i=0;i<shuzhu.length;i++){
                    if(shuzhu[i]>max)
                        max=shuzhu[i];
                    if(shuzhu[i]<min)
                        min=shuzhu[i];
                    sum+=shuzhu[i];
                }
                avg=sum/shuzhu.length;
                System.out.println("最小值"+min);
                System.out.println("最大值"+max);
                System.out.println("平均数"+avg);
            }
        }


  • 小石头LAY
    2018-03-07 17:07:36

    http://img2.mukewang.com/5a9fabb50001acb504320479.jpg修改一下圈出的部分看看。

  • 猫叔_3
    2018-03-07 16:36:45

    sum=shuzhu[i];改成max=shuzhu[i];