Java程序计算算术平均值

时间:2020-02-23 14:35:06  来源:igfitidea点击:

在本教程中,我们将看到简单的Java程序来计算算术均值。
以下是计算算术均值的简单算法。

  • 计算数组中的元素的总和
  • 然后除以数组中的元素的总数。
import java.util.Scanner;
 
public class ArithMeanCalc
{
    public static void main(String args[])
    {
        int cnt, i, sum=0, mean;
        int numArr[] = new int[50];
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Please enter count of numbers you want to Enter ? ");
        cnt = scan.nextInt();
		
        System.out.print("Enter " +cnt+ " Numbers : ");
        for(i=0; i<cnt; i++){
            numArr[i] = scan.nextInt();
            sum = sum + numArr[i];
        }
		
        mean = sum/cnt;
		
        System.out.print("Arithmetic Mean = " +mean);
    }
}

输出:

Please enter count of numbers you want to Enter ? 5
Enter 5 Numbers : 67 54 65 38 96
Arithmetic Mean = 64