Obtain 10 random integers from 0 to 20, and no repetition is required

Here is the programming house jb51 CC collects and arranges code fragments through the network.

Programming house Xiaobian now shares it with you and gives you a reference.

package Test;

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

public class Test01 {
	/**
	 * 编写一个程序:获取10个随机的0~20的整数,且要求不重复
	 * 分析:
	 * 	1、建立一个HashSet集合
	 * 	2、使用Random中的nextInt(n)方法随机产生10个整数
	 */
	public static void main(String[] args) {
		//	1、建立一个HashSet集合
		HashSet<Integer> hs = new HashSet<>();
		//2、使用Random类随机产生10个1~20的整数
		Random r = new Random();
		
		while(hs.size()<10) {
			hs.add(r.nextInt(21));
		}
		
		System.out.println(hs);
	}

}

The above is all the code content collected by the programming home (jb51. CC). I hope this article can help you solve the program development problems you encounter.

If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>