Java 中可以使用 Random 类的 nextInt(int n) 方法来生成 0 到指定 n-1 之间的随机数。因此,要生成 0 到 5 之间的随机数,可以使用以下代码:

Random rand = new Random();
int randomNumber = rand.nextInt(6); // 生成 0-5 之间的随机数

其中,nextInt() 方法的参数 n 表示生成的随机数的范围,但不包括 n 本身,因此需要传入 6 来生成 0 到 5 之间的随机数。

Java 生成 0-5 之间的随机数

原文地址: https://www.cveoy.top/t/topic/nVoW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录