import java.util.*;
public class CaiMi {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入选号上限:");
int max = Integer.parseInt(br.readLine());
Random r = new Random();
System.out.println("请输入选号个数:");
int n = Integer.parseInt(br.readLine());
while(n > max) {
System.out.println("错误,选号个数不能比上限大。请重新输入选号个数:");
n = Integer.parseInt(br.readLine());
}
HashSet hs = new HashSet();
for(int i = 1; i <= n; i++) {
int t = r.nextInt(max) + 1;
if(hs.add(t) == true)
System.out.print(t + ", ");
else
i--;
}
}}
没有评论:
发表评论