我的標準偏差是失敗的。當我輸入: 2 4 4 4 5 5 7 9 n代碼中的Java編程標準偏差錯誤
我沒有得到2.這是我的代碼。我相信一切正常,所以我不明白爲什麼我總是收到1.8284791953425266而不是2:
import java.util.Scanner;
public class stocks {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double currentNum = 0;
double numtotal = 0;
int count = 0;
double mean = 0;
double square = 0, squaretotal = 0, sd = 0;
System.out.println("Enter a series of double value numbers, ");
System.out.println("Enter anything other than a number to quit: ");
while (in.hasNextDouble())
{
currentNum = in.nextDouble();
numtotal = numtotal + currentNum;
count++;
mean = (double) numtotal/count;
square = Math.pow(currentNum - mean, 2.0);
squaretotal = squaretotal + square;
sd = Math.pow(squaretotal/count, 1/2.0);
}
System.out.println("The mean is: " +mean);
System.out.println("The standard deviation is: " +sd);
}
}
[該組的標準偏差爲2.13,實際](http://www.wolframalpha.com/input/?i=Standard+deviation%3A+ %7B2 + 4 + 4 + 4 + 5 + 5 + 7 + 9%7D) – Makoto 2013-03-05 05:03:05
我使用這個定義:http://en.wikipedia.org/wiki/Standard_deviation所以我除以n不是(n -1)在平方根符號 – LorrJ 2013-03-05 05:05:47