为啥我这个代码打印不出来任何东西

img

要求
随机数不应大于20;
代码应该只生成 10 个随机数。

下面是我的代码,可以compile并且运行,但是打印不出来任何东西。


import java.util.Random;

public class Test {
    public static void main(String[]args){
    
    Random ran=new Random(); //create random object
    
    int randomInt;
    randomInt=ran.nextInt();
        for(int i=0;i<10;i++){
            if((randomInt>0)&&(randomInt<20)){
        
            System.out.print("*");
        }
            
        }
    }
}

思路:
1.创建Random对象
2.定义一个接收随机数的对象
3.random.nextInt(20)中指定需要生成随机数的范围,阅读源码可知,这个范围介于[0,指定的正整数)(重要)

public static void main(String[] args) {
Random random = new Random();
for (int i = 1; i <= 10; i++) {
int randomInt = random.nextInt(20);
System.out.println("第" + i + "个,随机数" + randomInt);
}
}

randomInt=ran.nextInt();应该放循环体内,不然只会获得一个随机数

nextInt(20) 需要传入一个参数,这样才能返回20以内的数.否则他的范围远大于20