使用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);
        }*/
    }

}

 

posted @ 2018-07-31 19:53  张明洋  阅读(220)  评论(0编辑  收藏  举报