概述
给定一个整型数组,求其平均值。
代码
public static double avg(int[] array) {
int sum = 0;
for (int x : array) {
sum += x;
}
double avg = (double)sum / (double)array.length;
return avg;
}
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6};
System.out.println(avg(array));
}
运行结果