好吧所以我有一个代码设置得到一个有多达50个元素的数组的平均值,我想找到这些元素的标准偏差并显示在它显示均值的位置,我的代码如此远是
import java.util.Scanner;
public class caArray
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("How many numbers you want to calculate average upto 50 : ");
int n = input.nextInt();
int[] array = new int[50];
int sum = 0;
for (int m = 0; m < n; m++)
{
System.out.print("Number " + m + " : ");
array[m] = input.nextInt();
}
for (int m = 0; m < n; m++)
{
sum = array[m] + sum;
}
{
System.out.println("Total value of Numbers = " + sum);
}
{
double avg;
avg = sum / array.length;
System.out.println("Average of Numbers = " + avg); //calculate average value
}
}
}
我需要在此添加以获得一个程序中的标准偏差
编辑**我无法使用这些功能,因为我必须使用标准偏差fourmula与程序本身
答案 0 :(得分:1)
你可以用3种方法写出来
private double standardDeviation(double[] input) {
return Math.sqrt(variance(input));
}
private double variance(double[] input) {
double expectedVal = expectedValue(input);
double variance = 0;
for(int i = 0;i<input.length;++i) {
double buffer = input[i] - expectedVal;
variance += buffer * buffer;
}
return variance;
}
private double expectedValue(double[] input) {
double sum = 0;
for(int i = 0;i<input.length;++i) {
sum += input[i];
}
return sum/input.length;
}
希望它有效,如果我以正确的方式使用这些公式,我对此并不十分清醒。
但基本上你在计算中有这3个数学公式
答案 1 :(得分:0)
如果您不必自己编写,请查看Apache Commons Math。 stats documentation引用了如何派生standard deviations。
由于您必须自己编写,或许查看DescriptiveStatistics the source code会有所帮助(查找函数getStandardDeviation()
)