2024年4月26日发(作者:)
random的nextint方法
random的nextint方法是Java中Random类的一个方法,它可
以生成一个随机的整数。该方法需要一个整数作为参数,表示生成的
随机数的范围。例如,如果参数为10,则生成的随机数的范围为0
到9。如果参数为100,则生成的随机数的范围为0到99。
该方法的语法是:
public int nextInt(int bound)
其中,bound表示生成的随机数的范围。
该方法返回一个随机的整数,该整数大于等于0且小于bound。
下面是一个使用nextint方法的示例代码:
import ;
public class Main {
public static void main(String[] args) {
Random random = new Random();
// 生成一个随机数范围在0到9之间的整数
int num1 = t(10);
n(num1);
// 生成一个随机数范围在0到99之间的整数
int num2 = t(100);
n(num2);
}
}
- 1 -
运行该程序,将会输出两个随机数,它们的值分别在0到9和0
到99之间。
- 2 -
发布者:admin,转转请注明出处:http://www.yc00.com/news/1714144619a2390052.html
评论列表(0条)