package PTACZW; //随机函数 //输入一个n; //随机出项1~n的数 import java.util.Scanner; import java.util.Random; import java.util.Set; import java.util.HashSet; import java.util.ArrayList; public class Main{ public static void main(String[]args) { ArrayList<Integer> list=new ArrayList<>(); Scanner input=new Scanner(System.in); int n=input.nextInt(); HashSet<Integer> set = new HashSet<Integer>(); while (set.size() < n){ // 调用Math.random()方法 int num = (int) (Math.random() * (n)) + 1; if(!set.contains(num)) { list.add(num); } set.add(num); } for(int i:list) { System.out.print(i+" "); } } }
标签:set,java,int,生成,util,num,随机数,区间,import From: https://www.cnblogs.com/liliczw2209/p/17539397.html