/*写一个彩票的生成代码。
要求:1-33 随机选 7个无重复的数字
用数组保存7个数字,所有数字与以前的数字比较,如有重复重新生成。
测试算法用1-8 随机选7个*/
public class caipiao33 {
public static void main(String[] args) {
int i , j;
int[] redboll = new int[7];
int[] bulueboll = new int[1];
boolean[] used = new boolean[34];
Random show2 = new Random();
int bull = show2.nextInt(16) + 1;
bulueboll[0] = bull;
//Random show = new Random();
A: for (j = 0; j < 7; j++) {
i = show2.nextInt(33) + 1;
if (used[i]) {
j--;
continue A;
}
redboll[j] = i;
used[i] = true;
}
Arrays.sort(redboll);
System.out.println("随机选号红球是:" + Arrays.toString(redboll));
System.out.println("随机选号蓝球推荐号是:" + Arrays.toString(bulueboll));
}
}
第二种<hr>
public class caipiao3 {
public static void main(String[] args) {
int i , j,k=0;
int[] redboll = new int[11];
int[] bulueboll = new int[1];
boolean[] used = new boolean[34];
Random show2 = new Random();
int bull = show2.nextInt(6) + 1;
bulueboll[0] = bull;
Random show = new Random();
A: for (j = 0; j < 11; j++) {
i = show.nextInt(33) + 1;
Arrays.sort(redboll);
k=Arrays.binarySearch(redboll,redboll.length-j-1,redboll.length-1, i);
if(k>=0){
j--;
continue A;
}
redboll[j] = i;
}
Arrays.sort(redboll);
int[] answer=new int[7];
System.arraycopy(redboll, 5, answer, 0,6);
answer[answer.length-1]=show2.nextInt(7)+1;
System.out.println("随机选号号码是:"+Arrays.toString(answer));
System.out.println("随机选号蓝球推荐号是:" + Arrays.toString(bulueboll));
}
}