随机数的小案例:获取任意范围内的随机数

随机数的小案例:获取任意范围内的随机数

public class Demo1 {
	public static void main(String[] args) {
		// 1.键盘录入数据
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入开始数:");
		int start = sc.nextInt();
		System.out.println("请输入结束数:");
		int end = sc.nextInt();
		for (int i = 0; i < 10; i++) {
			int random = getRandom(start, end);
			System.out.println(random);
		}
	}
	public static int getRandom(int start, int end) {
		int num = (int) (Math.random() * (end - start + 1) + start);
		return num;
	}
}
运行结果如下:

请输入开始数:
900
请输入结束数:
1000
906
969
913
927
952
931
938
944
912
912


猜你喜欢

转载自blog.csdn.net/yes_or_no_123/article/details/79063514