彩票两种简单方式:用数组保存7个数字,所有数字与以前的数字比较,如有重复重新生成binarySearch

第一种简单实用些,其实用set集合的话就连判断语句都不用了.

/*写一个彩票的生成代码。
 要求: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));
}
}










  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值