求数组的平均值 Exercise07_08

 1 import java.util.Scanner;
 2 /**
 3  * @author 冰樱梦
 4  * 时间:2018年下半年
 5  * 题目:求数组的平均值
 6  *
 7  */
 8 public class Exercise07_08 {
 9     public static void main(String[] args){
10         Scanner input=new Scanner(System.in);
11         double[] array=new double[10];
12         System.out.println("输入10个double型的数");
13         for(int i=0;i<array.length;i++){
14             array[i]=input.nextDouble();
15         }
16         System.out.print(average(array));
17     }
18     
19     //求int类型的数组的平均值
20     public static int average(int[] array){
21         int total=0;
22         for(int i=0;i<array.length;i++){
23             total+=array[i];
24         }
25         return total/array.length;
26     }
27     
28     
29     //求double类型的数组的平均值
30     public static double average(double[] array){
31         double total=0;
32         for(int i=0;i<array.length;i++){
33             total+=array[i];
34         }
35         return total/array.length;
36     }
37 }
原文地址:https://www.cnblogs.com/cherrydream/p/10174066.html