/使用两种方式生成10-30之间的随机数。(Math.random/Random)/
import java.util.Random;
public class RandomDemo {
public static void main(String[] args) {
System.out.println("方法一:");
//方法一 Math.random
//首先得到0-20之间的随机数,在加10就得到10-30之间的随机数
for(int i=0;i<30;i++){
System.out.print((int)(Math.random()*100)%21+10+" ");
}
System.out.println("\n方法二:");
//方法二 Random 首先得到0-20之间的随机数,在加10就得到10-30之间的随机数
Random r=new Random();
for(int i=0;i<30;i++){
System.out.print(r.nextInt(21)+10+" ");
}
}
}
點擊查看更多內容
1人點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦