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