编写程序:随机20个两位数,找出这20个数中的最大值、最小值和平均值.

问题描述:

编写程序:随机20个两位数,找出这20个数中的最大值、最小值和平均值.
1个回答 分类:综合 2014-10-12

问题解答:

我来补答
public class ETest{
public static void main(String[] args){
int[] a = new int[20];
int sum = 0;
for(int i = 0;i < 20;i++){
a[i]= (int)(Math.random()*9+1)*10 + (int)(Math.random()*9+1);
System.out.println("第"+(i+1)+"个数:"+a[i]);
sum = sum + a[i];
}
sort(a);
System.out.println("平均 数:"+(sum/20));
}
public static void sort(int arr[]){
for(int j = 0;j < arr.length;j++){
int max = j;
for(int i = j;i < arr.length;i++){
if(arr[i] > arr[max]){
max = i;
}
}
int temp = arr[max];
arr[max] = arr[j];
arr[j] =temp;
}
System.out.println("最小 数:"+arr[arr.length-1]);
System.out.println("最大 数:"+arr[0]);
}
}
运行结果:
第1个数:86
第2个数:27
第3个数:76
第4个数:51
第5个数:95
第6个数:42
第7个数:24
第8个数:57
第9个数:49
第10个数:21
第11个数:71
第12个数:78
第13个数:57
第14个数:34
第15个数:77
第16个数:69
第17个数:95
第18个数:61
第19个数:56
第20个数:35
最小 数:21
最大 数:95
平均 数:58
——————————————————
注:每次运行结果不一样
 
 
展开全文阅读
剩余:2000
上一页:dc2inr3qec3r2
下一页:这样怎么写