指定范围内随机数产生
1.说明:产生随机数的代码封装为一个静态函数,可以直接调用函数,传入两个参数。两个参数用于指定范围。
参数1:start 为指定区间的开始,参数2:end 为指定区间的结束。
2.代码:
public class TestRandom {
public static void main(String[] args) {
//产生2到8之间的五个随机数
for(int i =1; i <= 5;i++){
System.out.println("第"+ i + "随机数:" + randomInt(0,2));
}
}
//封装一个函数,用于产生指定区间内的一个随机数整数。
public static int randomInt(int start,int end){
int m = (int)(Math.random()*(end - start + 1)) + start;
return m;
}
}
3.结果:

智商不是瓶颈,更需要坚韧和毅力。

浙公网安备 33010602011771号