如何获得标准差

时间:2016-10-08 03:30:36

标签: java loops math

idk如果我必须在循环中或循环中获得标准。也可以帮助我理解为什么我在循环内或外出它并且有什么区别。我也知道在这种情况下的标准偏差公式将是(输入 - 平均值)^ 2为第一个然后++为每个值然后添加所有那个并除以计数然后平方根。我只是不完全确定如何写它和把它放在哪里

import java.util.Scanner; 

public class readFromKeyboard { 

    public static void main(String[] args) { 


        Scanner input = new Scanner(System.in); 

        String inStr = input.next(); 
        int n; 
        int count=0; 
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE; 
        double average=0;
        int sum;
        double deviation = 0;

        while (!inStr.equals("EOL")) { 
          count++; 
          n = Integer.parseInt(inStr); 
          min = Math.min(min, n);
          max = Math.max(max, n);
          System.out.printf("%d ", n); 
          inStr = input.next(); 
          average += n;
        } 

        average = average/count;

        System.out.println("\n The average of these numbers is " + average);
        System.out.printf("The list has %d numbers\n", count); 
        System.out.printf("The minimum of the list is %d\n", min);
        System.out.printf("The maximum of the list is %d\n", max);


        input.close(); 


    } 
}

1 个答案:

答案 0 :(得分:1)

鉴于您已经计算了average,现在您可以为每个数字计算standard deviation

  1. 创建数组sd[]以存储standard deviation
  2. 每个号码sd[i] = (average - input_i) ^ 2
  3. 计算variance

    1. 对于standard deviation中的每个sd[],添加到变量temp
    2. temp除以输入总数
    3. 计算population standard deviation

      1. 平方根variance