首页javaintegerJava Data Type - 如何生成范围内的随机整数,滚动骰子

Java Data Type - 如何生成范围内的随机整数,滚动骰子

我们想知道如何生成范围内的随机整数,滚动骰子。

public class MainClass {
  public static void main(String[] args) {
    int roll;
    String msg = "Here are 100 random rolls of the dice:";
    System.out.println(msg);
    for (int i = 0; i < 100; i++) {
      roll = randomInt(1, 6);
      System.out.print(roll + " ");
    }
  }

  public static int randomInt(int low, int high) {
    int result = (int) (Math.random() * (high - low + 1)) + low;
    return result;
  }

}