集册 Java实例教程 创建随机数

创建随机数

欢马劈雪     最近更新时间:2020-01-02 10:19:05

445
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
下面的代码显示了如何执行此操作,将骰子游戏的值设置为1和6:
//时 代 J a v a - N o w J a v a . c o m 提 供

public class Main {

  public static void main(String[] args) {

    int low = 1;      // the lowest value in the range

    int high = 6;     // the highest value in the range

    int rnd = (int)(Math.random() * (high - low + 1)) + low;


    System.out.println(rnd);


  }


}