import java.util.Random;
//输出小于33的7个不相同的随机数
public class probability {
static Random random = new Random();
static int[] array = new int[7];
static int number;
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 0; i < array.length - 1; i++) {
number = (random.nextInt(33)) + 1;
if (i != 0) {
while (isRepeat(i, number)) {
number = random.nextInt(33) + 1;
}
}
array[i] = number;
}
array[6] = (int) (Math.random() * 16) + 1;
for (int j : array) {
System.out.println(j);
}
}
public static boolean isRepeat(int size, int number) {
boolean reqeat = false;
for (int i = 0; i < size; i++) {
if (array[i] == number) {
reqeat = true;
break;
}
}
return reqeat;
}
}
时间: 2024-12-28 18:43:56