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;
}
}
相关文章
- 07-31java中IO与数据库的小实例
- 07-31Java开发中经典的小实例-(while(参数){})
- 07-31Java开发中经典的小实例-(swich(){case:参数break;default: break;})
- 07-31Java开发中经典的小实例-(打印输入重复的值)
- 07-31Java开发中经典的小实例-(二分法)
- 07-31Java开发中经典的小实例-(能被3整除,并且十个数换一行)
- 07-31Java开发中经典的小实例-(比较输入数值的最大值、最小值和平均值)
- 07-31Java开发中经典的小实例-(do{}while())
- 07-31Java开发中经典的小实例-(随机数)
- 07-31Java开发中经典的小实例-(输入三个数字判断三角形类型)