编写一个程序,提示用户输入十个数字并显示其均值和标准差

时间:2018-12-08 07:26:09

标签: java mean standard-deviation

一个程序,提示用户输入十个数字并显示其均值和标准差。 n个数的平均值和标准偏差的计算方法如下:

enter image description here

求解方程式的代码

 import java.util.Scanner;
 public class Exercises5 {
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  double [] numbers = new double [10] ; 
  System.out.print("Enter ten numbers: ");
    for (int i = 0; i < 10; i++) 
        numbers[i] = input.nextDouble();

    double mean,deviation;

            mean = mean(numbers);
            deviation = std(numbers, mean);

            System.out.println("The mean is " + mean);
        System.out.printf("The standard deviation is %.5f\n", deviation);

  }
  public static double mean(double numArray[]){
        double sum = 0.0;
        int length = numArray.length;
        for(double num : numArray)
        sum += num;

        double mean = sum/length;

        return mean;
    }

    public static double std(double numArray[] , double mean{
    double standardDeviation = 0.0;
    int length = numArray.length;
    for(double num: numArray) {
        standardDeviation += Math.pow(num - mean, 2);
    }
   return Math.sqrt(standardDeviation /(length - 1));
  }
}

1 个答案:

答案 0 :(得分:1)

我唯一看到的问题是在第30行:

public static double std(double numArray[] , double mean{

double mean之后缺少右括号:

public static double std(double numArray[], double mean) {