编写一个Java应用程序,产生20个50-100之内的整数,并输出这20个数并找出最大数及最小数输出

思路:
首先调用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);
	}
}

运行结果截图:
在这里插入图片描述

原创文章 25 获赞 96 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_45092215/article/details/104596828