可以通過 Math.random()獲取一個[0,1)的double型隨機數;
int s=(int) (Math.random()*50);//生成一個0~50的隨機整數 int k=(int) (100-(Math.random()*50));//生成一個50~100的隨機整數
System.out.println(k);
System.out.println(s);
注意:千萬不要寫成 “ (int)Math.random()*100 ” 括號一定要括上否則結果為0.
可以通過 Math.random()獲取一個[0,1)的double型隨機數;
int s=(int) (Math.random()*50);//生成一個0~50的隨機整數 int k=(int) (100-(Math.random()*50));//生成一個50~100的隨機整數
System.out.println(k);
System.out.println(s);
注意:千萬不要寫成 “ (int)Math.random()*100 ” 括號一定要括上否則結果為0.
本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。