1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 double[] array = new double[10]; 9 10 System.out.print("Enter 10 numbers: "); 11 for(int i = 0; i < array.length; i++) 12 array[i] = input.nextDouble(); 13 14 input.close(); 15 16 System.out.println("The mean is " + getAverage(array)); 17 System.out.println("The standard deviation is " + getStandardDeviation(array)); 18 } 19 20 public static double getAverage(double[] array) 21 { 22 double sum = 0; 23 for(int i = 0; i < array.length; i++) 24 sum += array[i]; 25 double average = sum / array.length; 26 return average; 27 } 28 29 public static double getStandardDeviation(double[] array) 30 { 31 double sum1 = 0; 32 double sum2 = 0; 33 for(int i = 0; i < array.length; i++) 34 { 35 sum1 += array[i] * array[i]; 36 sum2 += array[i]; 37 } 38 double standardDeviation = Math.sqrt((sum1 - sum2 * sum2 / array.length) / (array.length - 1)); 39 return standardDeviation; 40 } 41 }
时间: 2024-11-08 19:50:48