编写程序:随机20个两位数,找出这20个数中的最大值、最小值和平均值.
网友回答
public class ETest{
public static void main(String[] args){
int[] a = new int[20];
int sum = 0;
for(int i = 0;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 int max = j;
for(int i = j;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——————————————————注:每次运行结果不一样