import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args){
// Scanner s = new Scanner(System.in);
// System.out.println("请输入一个数字");
// int num = s.nextInt();
Scanner s = new Scanner(System.in);
int sum = 0 ; //存放数组的和
int []a = new int[6] ;
for(int i = 0 ; i < a.length ; i++){
System.out.println("请输入数组的第"+(i+1)) ;
a[i] = s.nextInt();
sum += a[i];
}
//求最大值和最小值
int max = a[0] , min = a[0];
for(int i = 1 ;i < a.length ; i++){
if(max < a[i])
max = a[i];
if(min > a[i])
min = a[i];
}
//求数组的平均值
int avg = sum / a.length ;
for(int i = 0 ; i < a.length ; i++){
System.out.println(a[i]) ;
}
System.out.println("最大值:"+max+"最小值"+min+"平均值"+avg);
//实现数组倒置
for(int i = 0 ; i <= a.length / 2 - 1; i++){
int t = a[i];
a[i] = a[a.length - i - 1] ;
a[a.length-i-1] = t;
}
for(int i = 0 ; i < a.length ; i++){
System.out.println(a[i]) ;
}
}
}
Java数组的应用2:数组的最大,最小,求和,平均值,倒置
最新推荐文章于 2024-06-25 10:45:36 发布