我在这里很新, 目前我正在尝试用Java计算标准差(我已经用Google搜索了哈哈) 但是我在使其工作方面遇到了很多问题
我有10个值由用户输入,然后我必须计算标准偏差 到目前为止,我的理解归功于回复的人 是 我找到了阵列的意思 然后完成计算
double two = total[2];
double three = total[3];
double four = total[3];
double five = total[4];
double six = total[6];
double seven = total[7];
double eight = total[8];
double nine = total[9];
double ten = total[10];
double eleven = average_total;
mean = one + two + three + four + five + six + seven + eight + nine + ten + eleven;
mean = mean/11;
//one = one - mean;
//System.out.println("I really hope this prints out a value:" +one);
*/
//eleven = average_total - mean;
//eleven = Math.pow(average_total,average_total);
//stand_dev = (one + two + three + four + five + six + seven + eight + nine + ten + eleven);
//stand_dev = stand_dev - mean;
// stand_dev = (stand_dev - mean) * (stand_dev - mean);
// stand_dev = (stand_dev/11);
// stand_dev = Math.sqrt(stand_dev);
我已经将数据存储在10个值的数组中,但我不太确定 如何从数组中打印数据然后进行计算而不必在此处存储输入代码 数据一些我操纵过的地方
感谢您的时间,非常感谢:)
答案 0 :(得分:11)
有一个简单的公式可用于在每次添加数字时快速计算标准偏差。下面是一些实现该公式的代码,假设已经声明并填充了total[]
:
double powerSum1 = 0;
double powerSum2 = 0;
double stdev = 0;
for i = 0 to total.length {
powerSum1 += total[i];
powerSum2 += Math.pow(total[i], 2);
stdev = Math.sqrt(i*powerSum2 - Math.pow(powerSum1, 2))/i;
System.out.println(total[i]); // You specified that you needed to print
// each value of the array
}
System.out.println(stdev); // This could also be placed inside the loop
// for updates with each array value.
这个公式的优点在于,每次添加新值时都不必重新处理整个数组,并且不必存储数组的任何旧值,只需要存储的三个变量上面的代码。
答案 1 :(得分:5)
calculate mean of array.
loop through values
array value = (indexed value - mean)^2
calculate sum of the new array.
divide the sum by the array length
square root it
编辑:
我将向您展示如何循环遍历数组,只需使用不同的计算,所有内容都是相同的步骤。
// calculating mean.
int total = 0;
for(int i = 0; i < array.length; i++){
total += array[i]; // this is the calculation for summing up all the values
}
double mean = total / array.length;
EDIT2:
在阅读完代码后,您所做错误的部分就是您没有遍历这些值并正确地减去它。
又名这部分。
11 = average_total - mean;
eleven = Math.pow(average_total,average_total);
你需要这样做。
for(int i = 0; i < array.length; i++){
array[i] = Math.pow((array[i]-mean),2)
}
基本上你需要用newvalue = oldvalue - mean(average)来改变数组中的每个值。
然后计算总和...然后平方根。
答案 2 :(得分:2)
我不会为你解决问题,因为这看起来像是怎么回事,但我会试着通过给你一些假代码来帮助你指出正确的方向:
Loop over array i=1 to 10
Add each element to some other variable Total
End of Loop
Average = Total / 10 (required for your std. dev. equation)
现在你需要找出元素与均值的距离。易
Loop over array i = 1 to 10
Replace each element with its distance from Average Squared
Add to some variable differenceTotal
End of Loop
然后你有你的分子和分母术语,你的解决方案应该是显而易见的。希望这有点明确和有用。
答案 3 :(得分:1)
试试这个
import java.io.IOException;
public class StandardDeviationCalc {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
double [] values = {9, 2, 5, 4, 12, 7, 8, 11, 9, 3, 7, 4, 12, 5, 4, 10,
9, 6, 9, 4 }; //change input values here
double sum=0;
double finalsum = 0;
double average = 0;
for( double i : values) {
finalsum = (sum += i);
}
average = finalsum/(values.length);
System.out.println("Average: "+ average);
double sumX=0;
double finalsumX=0;
double[] x1_average = new double[2000];
for (int i = 0; i<values.length; i++){
double fvalue = (Math.pow((values[i] - average), 2));
x1_average[i]= fvalue;
System.out.println("test: "+ fvalue);
}
for(double i : x1_average) {
finalsumX = (sumX += i);
}
Double AverageX = finalsumX/(values.length);
System.out.println("E(X1-x1_average)^2/AverageX: "+ AverageX);
double SquareRoot = Math.sqrt(AverageX);
System.out.println("Standard Deviation: "+ SquareRoot);
}
}
你可以调整你想要添加用户输入的方式。代码很粗糙,因为我认为这是一个功课。尽量以自己的方式让它变得美好。希望它可以帮助你。
答案 4 :(得分:0)
你有什么尝试?...
您需要循环显示值以打印所有值
for(int i = 0; i < yourArray.length; i++)
{
System.out.println(yourArray[i]);
// Add your code to calculate the values you need for standard dev
}