JAVA-Random随机整数

image

package com.itheima;

import java.util.Random;

/*
Random的基本使用
 */
public class Scanner03 {
    public static void main(String[] args) {
        //Random():创建一个新的随机数生成器

        Random r=new Random();
//        //int nextInt(int bound);获取一个Int类型的随机数,参数bound表示,获取到的随机数在[0,bound)之间
////        int i = r.nextInt(20);
////        System.out.println(i);
//        for (int i=1;i<=10;i++) {
//            int j = r.nextInt(10);
//            System.out.println(j);
//        }

        //如何获取1-100之间的随机数呢?
//        int number = r.nextInt(101);//0-100

        int number = r.nextInt(100)+1;//0-100
        System.out.println(number);


    }
}

执行结果
91

Process finished with exit code 0
posted @ 2022-10-29 17:52  NiceTwocu  阅读(179)  评论(0编辑  收藏  举报