使用Set集合,生成1-25之内不重复的7个随机整数

//使用Set集合,生成1-25之内不重复的7个随机整数
public class z1to25 {
    public static void main(String[] args) {
        Set<Integer> s= new HashSet<>();
        Random r = new Random();
        for (int i = 0; s.size() <7 ; i++) {//因为SET是不重复集合,所以保证集合的长度满足条件退出循环
            s.add(r.nextInt(24)+1);
        }
        System.out.println(s);
      /*  for (int x:s) {
            System.out.print(" "+x);
        }*/
    }

}

猜你喜欢

转载自blog.csdn.net/zhangmy12138/article/details/81319487