【Java基础】求数组的最大值,最小值,和值,平均值
作者:互联网
1.实现
int[] arr = new int[10];
//赋值
for (int i = 0; i < arr.length; i++) {
arr[i] = (int)(Math.random()*90+10);
System.out.print(arr[i]+"\t");
}
int maxValue = arr[0];
int minValue = arr[0];
int sum = 0;
double avg = 0;
for (int i = 0; i < arr.length; i++) {
sum+=arr[i];
if (maxValue<arr[i]){
maxValue = arr[i];
}
if (minValue>arr[i]){
minValue = arr[i];
}
}
avg = sum*1.00/arr.length;
System.out.println("最大值"+maxValue);
System.out.println("最小值:"+minValue);
System.out.println("和为:"+sum);
System.out.println("平均:"+avg);
标签:arr,Java,int,最大值,System,最小值,println,sum,out 来源: https://www.cnblogs.com/zhishu/p/16614120.html