数组的平均值最大值最小值

时间:2021-09-24 14:43:10

import java.util.Scanner;

 

public class Work1 {

// 定义一个长度为10的整数数组,可用与保存用户通过控制台输入的10个整数

// 并计算它们的平均值、最大值、最小值

 

public static void main(String[] args) {

 

int[] a = new int[10];

int sum = 0;

int max = 0;

int min = 0;

Scanner sc = new Scanner(System.in);

for (int i = 0; i < a.length; i++) {

System.out.println("请输入第" + (i + 1) + "个数字");

int num = sc.nextInt();

a[i] = num;

}

for (int i = 0; i < a.length; i++) {

sum = sum + a[i];

}

System.out.println("元素的平均值是:" + (sum / a.length));

max = a[0];

min = a[0];

for (int i = 0; i < a.length; i++) {

if (max < a[i]) {

max = a[i];

}

if (min > a[i]) {

min = a[i];

}

}

System.out.println("最大值是:" + max);

System.out.println("最小值是:" + min);

 

}

}