日期:2014-05-20 浏览次数:20778 次
public class Ex1136 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入人数:");
int n = scanner.nextInt();
int b[] = new int[10];
for (int i = 0; i < 5; i++) {
int a = ((int) Math.random() * n + 1);
b[i] = a;
for (int j = 0; j < i; j++) {
if (b[i] == b[j]) {
i--;//这里死循环了!!!
break;
}
}
}
for (int i = 0; i < 5; i++) {
System.out.println(b[i]);
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入最大范围:");
int max = scanner.nextInt();
System.out.println("请输入个数:");
int num = scanner.nextInt();
Set<Integer> set=new HashSet<>();
while(set.size()<=num){
double random = Math.random();
int a = (int) (random * (double)max) + 1;//这里需要用double型进行乘操作
if(!set.contains(a)){
set.add(a);
}
}
//输出所有随机生成的数
for(Integer i:set){
System.out.println(i);
}
}