算法训练 整数平均值

算法训练 整数平均值  
时间限制:1.0s   内存限制:512.0MB
    
编写函数,求包含n个元素的整数数组中元素的平均值。要求在函数内部使用指针操纵数组元素,其中n个整数从键盘输入,输出为其平均值。
样例输入: (输入格式说明:5为输入数据的个数,3 4 0 0 2 是以空格隔开的5个整数)
5
3 4 0 0 2
样例输出:
1
样例输入: 
7
3 2 7 5 2 9 1
样例输出:
4
import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int a[]=new int[n];
        for(int i=0;i<n;i++){
            a[i]=sc.nextInt();
        }
        int sum=0;
        for(int i=0;i<n;i++)
            sum+=a[i];
        System.out.println(sum/n);

    }
    

}
原文地址:https://www.cnblogs.com/watchfree/p/5348174.html