沉吟丹青 2019-11-01 12:44 采纳率: 70%
浏览 851
已采纳

怎样用 random 生成一个在50到1000之间且是50倍数的数 返回int

怎样在一个方法中
用 random 生成一个在50到1000之间,且是50倍数的数
返回一个int

我这样写是不是不对呀?怎么修改呢?

public static int getBalloonValue() {
Random random = new Random();
int balloonValue = random.nextInt(1);
if (balloonValue % 50 == 0 || balloonValue > 50 || balloonValue < 1000) {
}
return balloonValue;
}

  • 写回答

2条回答 默认 最新

  • 默默a20 2019-11-01 13:25
    关注

    这么简单
    ( (int)(Math.random()*20)+1)*50
    (int)(Math.random()*20) 确保生成 0 到19 的 的20个int 类型
    +1 就是偏移到1-20
    然后*50 就是50-1000 了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?