不输出正确的均值或方差

时间:2016-03-09 08:30:15

标签: java arrays for-loop arraylist while-loop

我的任务是取数字,每行一个并计算平均值和中位数和方差以及标准偏差。输入null或输入500个数字时,while循环停止。我计算了中位数和标准差,但由于某些原因,我的方差和均值输出是错误的。此外,如果您可以帮助我使用while循环的参数,也可以在输入500个数字时结束。

我的意见:

  

0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,1.0,2.0,2.0,2.0,2.0,2.0,2.0,3.0,3.0,3.0,3.0,3.0,4.0 ,4.0,4.0,4.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0,6.0,6.0,6.0,6.0,7.0,7.0,7.0,7.0,7.0,8.0,8.0,8.0,8.0 ,8.0,8.0,8.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,11.0,11.0,11.0,11.0,12.0,12.0,12.0 ,12.0,12.0,12.0,12.0,12.0,12.0,13.0,13.0,13.0,13.0,13.0,13.0,13.0,13.0,14.0,14.0,14.0,14.0,14.0,14.0,15.0,15.0,15.0,15.0,15.0 ,15.0,15.0,16.0,16.0,16.0,17.0,17.0,17.0,17.0,18.0,18.0,18.0,18.0,18.0,18.0,18.0,19.0,19.0,19.0,20.0,20.0,20.0,20.0,20.0,20.0 ,20.0,21.0,21.0,21.0,21.0,21.0,21.0,21.0,21.0,22.0,22.0,22.0,22.0,22.0,22.0,22.0,23.0,23.0,23.0,23.0,23.0,24.0,24.0,24.0,24.0 ,24.0,25.0,25.0,25.0,25.0,25.0,25.0,26.0,26.0,26.0,26.0,26.0,26.0,26.0,26.0,27.0,27.0,27.0,28.0,28.0,28.0,29.0,29.0,29.0,29.0 ,29.0,30.0, 30.0,30.0,30.0,30.0,30.0,31.0,31.0,31.0,31.0,33.0,33.0,33.0,33.0,33.0,34.0,34.0,34.0,34.0,34.0,34.0,34.0,35.0,35.0,35.0,35.0, 35.0,36.0,36.0,36.0,37.0,37.0,37.0,38.0,38.0,38.0,38.0,39.0,39.0,39.0,39.0,39.0,39.0,39.0,40.0,40.0,40.0,41.0,41.0,41.0,41.0, 41.0,42.0,42.0,42.0,42.0,43.0,43.0,43.0,43.0,44.0,44.0,44.0,45.0,45.0,45.0,45.0,45.0,46.0,46.0,46.0,46.0,46.0,46.0,47.0,47.0, 47.0,47.0,47.0,49.0,49.0,49.0,49.0,49.0,50.0,50.0,51.0,51.0,51.0,51.0,51.0,52.0,52.0,52.0,52.0,52.0,53.0,53.0,53.0,53.0,54.0, 54.0,54.0,55.0,55.0,55.0,56.0,56.0,56.0,56.0,56.0,56.0,57.0,57.0,58.0,58.0,58.0,58.0,58.0,59.0,59.0,59.0,59.0,59.0,59.0,60.0, 60.0,60.0,60.0,60.0,60.0,61.0,61.0,61.0,61.0,61.0,61.0,61.0,61.0,62.0,62.0,62.0,62.0,63.0,63.0,63.0,64.0,64.0,64.0,64.0,64.0, 64.0,64.0,64.0,65.0,65.0,65.0,65.0,65.0,65.0,66.0,66.0,66.0,66.0,66.0,66.0,67.0,67.0 ,67.0,68.0,68.0,68.0,68.0,68.0,68.0,68.0,68.0,68.0,69.0,69.0,69.0,69.0,69.0,69.0,70.0,70.0,70.0,70.0,70.0,70.0,70.0,71.0,71.0 ,71.0,71.0,71.0,71.0,71.0,72.0,72.0,72.0,72.0,72.0,72.0,72.0,72.0,72.0,73.0,73.0,73.0,73.0,74.0,74.0,75.0,75.0,75.0,75.0,76.0 ,76.0,76.0,76.0,76.0,77.0,77.0,77.0,77.0,77.0,77.0,77.0,78.0,78.0,78.0,79.0,79.0,79.0,79.0,79.0,79.0,79.0,79.0,79.0,80.0,80.0 ,80.0,80.0,81.0,81.0,81.0,82.0,82.0,82.0,82.0,82.0,82.0,83.0,83.0,83.0,83.0,84.0,84.0,84.0,84.0,84.0,85.0,85.0,85.0,85.0,85.0 ,85.0,85.0,86.0,86.0,87.0,87.0,87.0,87.0,87.0,87.0,88.0,88.0,88.0,88.0,88.0,89.0,89.0,89.0,89.0,90.0,90.0,90.0,99.0,91.0,91.0 ,91.0,92.0,92.0,92.0,93.0,93.0,93.0,93.0,94.0,94.0,94.0,94.0,94.0,95.0,95.0,95.0,96.0,96.0,96.0,96.0,96.0,97.0,97.0,99.0,98.0 ,98.0,98.0,98.0,99.0,99.0,99.0,100.0,100.0

我的输出:

  

平均值:47.21
  中位数:47.00
  差异:857.34   标准差:29.28

预期产出:

  

平均值:47.27
  差异:856.71
  标准差:29.27

BufferedReader stdin =
                    new BufferedReader (new InputStreamReader (System.in));
            NumberFormat nf = new DecimalFormat ("0.00");
            nf.setMinimumFractionDigits (2);//Sets Min digits
            nf.setMaximumFractionDigits (2);//Sets Max digits
            String inputValue;
            double sum = 0;
            double counter = 0;
            double median = 0;
            double variance = 0;
            double varianceCounter = 0;






            List<String> input = new ArrayList<String>();
            List<Double> numbers = new ArrayList<Double>();


            while((inputValue = stdin.readLine()) != null && !inputValue.equals("")){
                input.add(inputValue);
                numbers.add (Double.parseDouble(inputValue));        
            }
                 for(int i = 0; i < input.size(); i++){
                    sum += (numbers.get(i));
                    counter++;  
                }

            Collections.sort(numbers);
            for(int i = 0; i < numbers.size(); i++){
            if  (numbers.size() % 2 == 0){
                median = (numbers.get(numbers.size()/2-1) + numbers.get(numbers.size()/2))/2;
            }
            else {
                median = (numbers.get((numbers.size())/2));
            }
            }
            for(int i = 0; i < input.size(); i++){
                    variance += numbers.get(i) * numbers.get(i);
                    varianceCounter++;
            }






            System.out.println ("Mean: " + (nf.format(sum/counter)));
            System.out.println ("Median: " + (nf.format(median)));
            System.out.println ("Variance: " + (nf.format((variance/varianceCounter)-(sum*sum)/(varianceCounter*varianceCounter))));    
    System.out.println ("Standard Deviation: " + (nf.format(Math.sqrt((variance/varianceCounter)-(sum*sum)/(varianceCounter*varianceCounter)))));           
    }
    }

1 个答案:

答案 0 :(得分:1)

这是在IDE中使用格式化程序(如果不是调试器)有帮助的地方。

你遇到的问题是你在每个输入上总结所有值。

while ((inputValue = stdin.readLine()) != null && !inputValue.equals("")){
    input.add(inputValue);
    numbers.add (Double.parseDouble(inputValue));        
    // sum all the values on every input i.e. 1 1 2 1 2 3 1 2 3 4
    for (int i = 0; i < input.size(); i++){
        sum += (numbers.get(i));
        counter++;  
    }

你应该做的是阅读所有输入,然后才对所有值进行求和。

while ((inputValue = stdin.readLine()) != null && !inputValue.equals("")){
    input.add(inputValue);
    numbers.add (Double.parseDouble(inputValue));        
}
// sum all the values AFTER all the input. i.e. 1 2 3 4
for (int i = 0; i < input.size(); i++){
    sum += (numbers.get(i));
    counter++;  
}

BTW计数器应始终为input.size()