乱数を取得する方法ランダムクラスオブジェクトメソッドjava 0913

乱数を取得する方法ランダムクラスオブジェクトメソッドjava 0913

ランダムクラス

このクラスをインスタンス化してオブジェクトを取得します

オブジェクトメソッドを使用して整数を取得する

コード

import java.util.Random;

public class test {
    
    
    public static void main(String[] args) {
    
    

        Random random = new Random();
        // 测试随机出来的结果范围
        for(int i=0;i<20;i++){
    
    
            int res = random.nextInt(3);
            System.out.print(res);
        }

    }
}

効果

22012221000112211022

結論として

ランダムオブジェクトのNextInt(整数パラメーター)

整数パラメータ-1、整数に0を取得できます

ビデオ

https://www.ixigua.com/6871841743608906254?logTag=HyaquIQxNlY-Nj0FpsOqo

おすすめ

転載: blog.csdn.net/ifubing/article/details/108561647