Java语言编写一个程序,获取10个1至20的随机数,要求随机数不能重复。

【Java】编写一个程序,获取10个1至20的随机数,要求随机数不能重复。

/*作者:龙蝶
*日期:2020年4月4日
*/
import java.util.HashSet;
import java.util.Random;

 public class HashSetTest {
    
    

	public static void main(String[] args) {
    
          
	 	
	 	Random random=new Random(); 
		HashSet<Integer> hashset=new HashSet<Integer>();

       		 while(hashset.size()<10){
    
    
			
			hashset.add(random.nextInt(20)+1);
			}

		System.out.println(hashset);

}
}

猜你喜欢

转载自blog.csdn.net/m0_46399068/article/details/105313177