平均值和标准差

时间:2017-03-14 20:51:42

标签: java

我正在上课这个任务,我似乎无法看到我出错的地方。我把相同的输入作为我教授的例子,但我有不同的输出。输入1,2,3,4.5,5.6,6,7,8,9,10的平均值为5.61,标准差为2.99794373974304。但我得到的平均值5.61是正确的,但标准差为2.9963144027287916。不知道我哪里错了。有任何想法吗?没有找人为我这样做。我只需要知道我哪里出错了。

public static void main(String[] args) {
    //
    Scanner input = new Scanner(System.in);
    //User Input 1-10
    System.out.print("Enter a number: ");
    double inputA = input.nextDouble();
    System.out.print("Enter a number: ");
    double inputB = input.nextDouble();
    System.out.print("Enter a number: ");
    double inputC = input.nextDouble();
    System.out.print("Enter a number: ");
    double inputD = input.nextDouble();
    System.out.print("Enter a number: ");
    double inputE = input.nextDouble();
    System.out.print("Enter a number: ");
    double inputF = input.nextDouble();
    System.out.print("Enter a number: ");
    double inputG = input.nextDouble();
    System.out.print("Enter a number: ");
    double inputH = input.nextDouble();
    System.out.print("Enter a number: ");
    double inputI = input.nextDouble();
    System.out.print("Enter a number: ");
    double inputJ = input.nextDouble();

    //Formula for Mean
    double mean = (inputA + inputB + inputC + inputD + inputE + inputF + inputG + inputH + inputI + inputJ) / 10;

    //Formula for standard deviation
    //Subtract each input by the mean
    double m1 = (inputA - mean);
    double m2 = (inputB - mean);
    double m3 = (inputC - mean);
    double m4 = (inputD - mean);
    double m5 = (inputE - mean);
    double m6 = (inputF - mean);
    double m7 = (inputG - mean);
    double m8 = (inputH - mean);
    double m9 = (inputI - mean);
    double m10 = (inputJ - mean);

    //Square each m variable
    double v1 = (Math.pow(m1, 2));
    double v2 = (Math.pow(m2, 2));
    double v3 = (Math.pow(m3, 2));
    double v4 = (Math.pow(m4, 2));
    double v5 = (Math.pow(m5, 2));
    double v6 = (Math.pow(m6, 2));
    double v7 = (Math.pow(m7, 2));
    double v8 = (Math.pow(m8, 2));
    double v9 = (Math.pow(m9, 2));
    double v10 = (Math.pow(m10, 2));

    //Add all the sums squared sums together
    double var = (v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10);


    //divide by the number of variables to get Variance
    double var1 = (var / 10);
    double var2 = (var % 10);
    double var3 = (var1 + var2);

    //Standard deviation is the square root of the Variance
    double dev = (Math.sqrt(var3));

    //Mean and Deviation print
    System.out.println("The mean is " + mean);
    System.out.println("the deviation is: " + dev);
}

修改了我的编码。但我仍然不在。任何人都可以帮我找到更准确的答案吗?我仍然难倒!

  public static void main(String[] args) {



    double[] arrayM = new double[10];
    double[] arrayV = new double[10];

    double sum = 0;

    for (int index = 0; index < arrayM.length ; index ++) {
        System.out.print("Enter a number: ");
        Scanner input = new Scanner(System.in);
        double in = input.nextDouble();
        arrayM[index] = in;
        sum += in;    
    }
      //final mean and variant
        double mean = sum/arrayM.length;
        double vari = 0;

    for (int index = 0; index < arrayM.length ; index ++) {
        double var=((index+1)-mean)^2;
        arrayV[index] = var;
        vari += var;
    }
        double variant = vari/arrayM.length;
     //final deviation   

     double devi= Math.sqrt(variant);




    System.out.print("The mean is: "+mean);
    System.out.print("The standard deviation is: "+devi);

 }

}

所以它表示在double var =((index + 1)-mean)^ 2中的平均值将不起作用。我怎么写它所以它是 - 意思

最终编辑:我明白了。这是最终的代码。

    public class TESTPROJECT {

  /**
 * @param args the command line arguments
 */
 public static void main(String[] args) {
    // TODO code application logic here



 //Arrays
    Scanner input = new Scanner(System.in);
        double[] arrayM = new double[10];
        double[] arrayV = new double[10];


    //Sum 
    double sum = 0;


    //Sum array
    for (int index = 0; index < arrayM.length ; index ++) {
        System.out.print("Enter a number: ");
        double in = input.nextDouble();
        arrayM[index] = (in);
        sum += in;    
    }

    //Mean
    double mean = sum/arrayM.length;

    for (int index = 0; index < arrayM.length ; index ++) {
        double var = arrayM[index] - mean;
        arrayV[index] = var;
    }

    //Variant Loop
    double sqre = 0; 

    for(int index = 0; index < arrayV.length; index++) {
        double sqr = Math.pow(arrayV[index],2);
        sqre += sqr;
    }

    //Deviation cal
    double variant = sqre/arrayM.length;

     //final deviation   

     double devi= Math.sqrt(variant);



     //ouput
      System.out.println("The mean is: "+mean);
      System.out.println("The standard deviation is: "+devi);

 }
 }

1 个答案:

答案 0 :(得分:0)

您接近解决方案(但您的实施不是很方便)。 请参阅您给出的方差和偏差的定义。 这应该在开始编码之前完成。如果找不到定义,请尝试此处:https://en.wikipedia.org/wiki/Standard_deviation

你在这里错了:

//divide by the number of variables to get Variance
double var1 = (var / 10);
double var2 = (var % 10); // why ?
double var3 = (var1 + var2); // and why ??

顺便说一句,使用数组和循环可能会有所帮助,这里有一个片段:

    double[] valuesArray = new double[] {1.0,2,3,4.5,5.6,6,7,8,9,10};

    double mean = 0;
    double variance = 0;
    double deviation = 0;

    for (int index = 0; index < valuesArray.length ; index ++) {
        mean += valuesArray[index];

        System.out.println("value "+ index + " is : "+valuesArray[index]);
    }

    System.out.println("Sum : "+mean);

    // divide sum by number of values
    mean = mean / ((double) valuesArray.length);

    System.out.println("Mean : "+mean);