简体   繁体   English

在java中计算标准偏差

[英]Calculating standard deviation in java

Do I calculate my standard deviation in the loop.我是否在循环中计算我的标准偏差。 I know the formula for calculate the standard deviation, I just want to know how to take user inputs for the calculation.我知道计算标准偏差的公式,我只想知道如何获取用户输入进行计算。 I'm new to programming so please explain everything.我是编程新手,所以请解释一切。 I also don't mind my sorry attempt at trying to write out Standard deviation formula it wasn't meant to work just to understand SD more.我也不介意我试图写出标准偏差公式的抱歉尝试,它并不是为了更多地理解 SD 而工作。

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 i;
    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;
      // really bad attempt here
      deviation = math.pow(n / average,2) ++ / mean sqrt(); 
    } 
    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(); 
  } 
}

A few issues with your code first:首先你的代码有几个问题:

Your use of average is very confusing, since you are first using it as an accumulator and only then dividing it by the number of elements.您对average使用非常令人困惑,因为您首先将其用作累加器,然后才将其除以元素数。 Far better style would be to use a separate accumulator variable to calculate the sum, and then calculate average outside the for loop.更好的风格是使用单独的累加器变量来计算总和,然后在 for 循环外计算平均值。

That being said, once average is calculated, you can then loop through your values again, and keep an accumulator variable that for each number increases by the square of the difference between it and the mean, along the lines of:话虽如此,一旦计算出平均值,您就可以再次遍历您的值,并保留一个累加器变量,对于每个数字,该变量增加其与平均值之间的差值的平方,如下所示:

varianceSum += Math.pow(num - average, 2);

Once all numbers are summed up, you can divide varianceSum by count to get the variance (perhaps with a check to avoid division by 0), and use Math.sqrt() to find the standard deviation.将所有数字相加后,您可以将varianceSum总和除以count以获得方差(可能检查以避免除以 0),并使用Math.sqrt()找到标准偏差。

As for getting input from the user, you will need to call input.nextInt() repeatedly for every number.至于从用户那里获取输入,您需要为每个数字重复调用input.nextInt() You can either have the user enter the number of inputs at the beginning, and then loop through them in a for loop, or you can read until you reach the end of the input using Scanner's hasNext() method.您可以让用户在开头输入输入的数量,然后在 for 循环中遍历它们,或者您可以使用 Scanner 的hasNext()方法读取直到到达输入的末尾。

You should call input.next();你应该调用input.next(); inside the loop to get user input repeatedly.在循环内部重复获取用户输入。

For the record, you can use in.nextInt() instead of input.next();为了记录,您可以使用in.nextInt()而不是input.next(); , as you are dealing with integers here. ,因为您在这里处理整数。 Your calculation of deviation is not clear.你的deviation计算不清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM