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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP