我只是对如何存储用户输入的数字数组感到困惑。用户可以在数组中输入他们想要的数字,但它本身并不要求输入数字。
import java.util.Scanner;
public class Exercise_0_2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("How many numbers: ");
int n = input.nextInt();
System.out.println("Enter " + n + " numbers : ");
double[] numbers = new double[n];
//Call and print methods
System.out.println("Mean: " + mean(numbers, n));
System.out.println("Standard Deviation: " + deviation(numbers, n));
}
// Calculate Mean
public static double mean(double[] numbers, int n) {
double sum = 0;
for (int i = 0; i < n; i++) {
sum += numbers[i];
}
double mean = sum / n;
return mean;
}
//Calculate deviation from the mean
public static double deviation(double[] numbers, int n) {
double mean = mean(numbers, n);
double sqSum = 0;
for (int i = 0; i < n; i++) {
sqSum += numbers[i] * numbers[i];
}
double variance = sqSum / n - mean * mean;
double sd = Math.sqrt(variance);
return sd;
}
}
阿波罗的战车
慕森王
相关分类