蓝桥杯 整数平均值 JAVA实现

    技术2022-07-11  130

    问题描述 编写函数,求包含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 解题思路:定义个数和新数组,用循环录入数组元素且求出数组和,在循环外定义一个和等于零,用循环求出的和除以个数得出结果。 ———————————————— public class Main {

    public static void main(String[] args) {

    Scanner sc=new Scanner(System.in); System.out.println(“请输入个数:”); int n=sc.nextInt(); int he=0; int[]a=new int[n]; for (int i = 0; i < n; i++) { a[i]=sc.nextInt(); he=he+a[i]; } int result=he/n; System.out.println(result); }

    }

    Processed: 0.013, SQL: 9