HashSet<Integer> hs = new HashSet<>(); Random r = new Random(); while(hs.size() < 10) { int num = r.nextInt(20) + 1; hs.add(num); } 然后遍历打印就行了