java Math.round() 随机数
答案:5 悬赏:0
解决时间 2021-02-13 09:40
- 提问者网友:芷芹
- 2021-02-13 06:12
java Math.round() 随机数
最佳答案
- 二级知识专家网友:青灯壁纸妹
- 2021-02-13 07:34
我先问个问题,生成随机数的方法是round么? 是random吧?
这里有2个办法,一个是用Math,另一个是用Random类。
1、Math类
DecimalFormat df = new DecimalFormat("000");
int temp = (int) Math.random()*1000;
String randomNum = df.format(temp);
System.out.println(randomNum);
2、Random类
DecimalFormat df = new DecimalFormat("000");
Random r = new Random();
int temp = r.nextInt(1000);
String randomNum = df.format(temp);
System.out.println(randomNum);
这里有2个办法,一个是用Math,另一个是用Random类。
1、Math类
DecimalFormat df = new DecimalFormat("000");
int temp = (int) Math.random()*1000;
String randomNum = df.format(temp);
System.out.println(randomNum);
2、Random类
DecimalFormat df = new DecimalFormat("000");
Random r = new Random();
int temp = r.nextInt(1000);
String randomNum = df.format(temp);
System.out.println(randomNum);
全部回答
- 1楼网友:狙击你的心
- 2021-02-13 10:42
(int)(Math.round() *100)
这样来试试
- 2楼网友:一场云烟
- 2021-02-13 10:35
楼上少写了,x<(int)(Math.random() *1000)&&x>(int)(Math.random())
- 3楼网友:初心未变
- 2021-02-13 09:51
能不能用别的函数啊!
public static void main(String[] args) {
Random r = new Random() ;
String s="";
for(int i=0;i<3;i++){
s=s+r.nextInt(10);
}
System.out.println(s);
}
- 4楼网友:风格单纯
- 2021-02-13 08:18
nextInt(int n)
返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值。
因此可以:
System.out.println(new Random().nextInt(999));
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯
• 手机登qq时,显示手机磁盘不足,清理后重新登 |
• 刺客的套装怎么选啊? |