码迷,mamicode.com
首页 > 其他好文 > 详细

HW5.21

时间:2016-08-21 15:10:25      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

技术分享

 

 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 }

 

HW5.21

标签:

原文地址:http://www.cnblogs.com/wood-python/p/5792676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!