Java程序来计算Java中数字的平均值
一组数字的平均值是它们的总和除以数量。可以定义为-
average = sum of all values / number of values
在这里,我们将学习如何以编程方式计算平均值。
算法
1. Collect integer values in an array A of size N. 2. Add all values of A. 3. Divide the output of Step 2 with N. 4. Display the output of Step 3 as average.
示例
public class AverageOfNNumbers {
public static void main(String args[]){
int i,total;
int a[] = {0,6,9,2,7};
int n = 5;
total = 0;
for(i=0; i<n; i++) {
total += a[i];
}
System.out.println("Average ::"+ total/(float)n);
}
}输出结果
Average ::4.8
热门推荐
1 甄嬛传祝福语简短
3 敏字的祝福语简短
10 给医生简短新年祝福语
11 新年发给客户祝福语简短
12 伤者出院祝福语简短大全
13 新人结婚父母祝福语简短
14 新年的好祝福语简短
15 新婚祝福语简短十六字
16 兔年祝福语简短100字
17 新年的祝福语简短的
18 结婚婚庆祝福语简短