java随机生成1~5的整数

共有两种方法

random.nextInt和Math.random()

public class RandomNum {
    public static void main(String[] args) {
        Random r = new Random();
        for (int i = 0; i < 10; i++) {
            int num = (int) (Math.random() * 5 + 1);
            int a = r.nextInt(5) + 1;
            System.out.println("a:" + a);
            System.out.println("num:" + num);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/sinat_29774479/article/details/79699801