思路:
首先调用random()方法产生随机数,用数组存储,然后用两个for循环求出其中的最大值和最小值,最后打印输出这两个最值。
代码实现:
public class Test {public static void main(String args[]){int a[] = new int[20];System.out.println("20个随机整数为:");for(int i = 0; i < 20; i++){int random_num = (int)(Math.random() * 50 + 51);System.out.println(random_num);a[i] = random_num;}int max = 0,min = 100;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);}
}
运行结果截图: