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

package cn.ls.lanqiao;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class Test {

	public static void main(String[] args) {

		Random r = new Random();
		Set<Integer> a = new HashSet<>();
		for (int i = 0; a.size() < 5; i++) {
			int w = r.nextInt(100);// 包括0但不包含100[0,99]
			System.out.println(w);
			a.add(w + 1);// [1,100]
		}
		System.out.println(a);
		for (int x : a) {
			System.out.print(" " + x);
		}
	}
}

 

posted @ 2020-02-07 21:36  小帅学java  阅读(42)  评论(0)    收藏  举报