为什么我的平均值不会以他们应该的方式打印出来?

时间:2018-05-11 02:50:03

标签: java arrays sorting number-formatting drjava

我必须制作一个程序,打印出体操比赛中每个参赛者的平均分数。我必须对数组中的元素进行排序,然后删除每个数组的最低和最高分数。我想我已经找到了那个部分(如果我错了,请纠正我!),但我的平均值打印为4.6900,0.0000和0.9000,当它们应该是5.8625,0.0000和1.0000时。这就是我的代码:

import java.util.*;
import java.text.*;
public class EZD_gymnastics
{
  public static void main (String [] args)
  {
    double comp1[] = {8.7, 6.5, 0.1, 3.2, 5.7, 9.9, 8.3, 6.5, 6.5, 1.5};
    double comp2[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
    double comp3[] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};

    NumberFormat fmt = NumberFormat.getNumberInstance();
    fmt.setMaximumFractionDigits(4);
    fmt.setMinimumFractionDigits(4);

    Arrays.sort(comp1);

    double sum = 0;
    double sum2 = 0;
    double sum3 = 0;
    double mean = 0;
    double mean2 = 0;
    double mean3 = 0;

    for(int a = 1; a < 9; a++)
    {
      sum =  sum + comp1[a];
      mean = sum / comp1.length;
    }

    String s = fmt.format(mean);
    System.out.println("For Competitor #1, the average is " + s + ".");

    for(int b = 1; b < 9; b++)
    {
      sum2 =  sum2 + comp2[b];
      mean2 = sum2 / comp2.length;
    }

    String t = fmt.format(mean2);
    System.out.println("For Competitor #2, the average is " + t + ".");


    for(int c = 1; c <9; c++)
    {
      sum3 =  sum3 + comp3[c];
      mean3 = sum3 / comp3.length;
    }

    String u = fmt.format(mean3);
    System.out.println("For Competitor #3, the average is " + u + ".");
  }
}

0 个答案:

没有答案