Java Random类中 nextInt(n)方法,返回的随机数范围为 [0, n),整体+1之后变成 [1, n+1), 也就是 [1, n]

 

import java.util.Random

public class Demo01{
    public static void main(String[] args){
        int n=5;

        Random r = new Random();
        int result = r.nextInt(n) + 1;
        System.out.println(result);
    }
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-19
  • 2022-12-23
  • 2022-03-02
猜你喜欢
  • 2021-08-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-07
  • 2021-08-16
  • 2022-12-23
相关资源
相似解决方案