gpt4 book ai didi

java - 用java计算平均值和标准差

转载 作者:行者123 更新时间:2023-12-02 03:22:27 27 4
gpt4 key购买 nike

我在计算平均值时没有遇到问题,但我的标准差总是有点偏离。我很困惑。对于标准差,我使用 for 循环返回填充了用户提示值的数组,并从每个值中减去平均值,然后对其进行平方。然后再进行一个 for 循环,将它们全部加在一起,除以值的数量,然后开平方。我错了什么?

import java.util.Scanner;

public class Assignment4
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);


System.out.println("Please enter the number of items:"); // PROMPTS USER FOR # OF ITEMS
double size = scan.nextDouble();

double meanSum = 0;
double deviationSum = 0;
double array[] = new double [(int) size];


System.out.println("Please enter the items");
for (int i = 0; i < size; i++)
{
array[i]= scan.nextDouble();
}


for (int i = 0; i < size; i++)
{
meanSum += array[i];
}

double mean = ((meanSum / size));
System.out.println("The mean is: " + mean);




//STANDARD DEVIATION CALCULATION
for (int i = 0; i < size; i++)
{
array[i] = (Math.pow((array[i] - mean), 2));
}
for (int i = 0; i < size; i++)
{
deviationSum += array[i];
}

double variance = ((deviationSum / size));

double standardDeviation = Math.sqrt(variance);
System.out.println("The standard deviation is: " + standardDeviation);

}

}

最佳答案

根据您将答案与什么进行比较,它可能会使用更正,例如 Bessel's correction 。换句话说,不是除以 size,而是除以 size-1。另请参阅thisthis

关于java - 用java计算平均值和标准差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39431844/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com