一个程序,提示用户输入十个数字并显示其均值和标准差。 n个数的平均值和标准偏差的计算方法如下:
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));
}
}
答案 0 :(得分:1)
我唯一看到的问题是在第30行:
public static double std(double numArray[] , double mean{
在double mean
之后缺少右括号:
public static double std(double numArray[], double mean) {