public class Test { public static void main(String[] args){ Random ran = new Random(); Setset = new HashSet (); while(set.size()==10?false:true){ int num = ran.nextInt(20)+1; set.add(num); } Iterator it = set.iterator(); int count = 0; while(it.hasNext()){ System.out.println("第"+ ++count +"个随机数 =="+it.next()); } } }