public class Lottery {
/**
* 产生一注7个不同号码的彩票 并返回彩票!
*
* @return
*/
public int[] creatLottery() {
int[] lottery = new int[7];
Random ran = new Random();// 随机源
for (int i = 0; i < lottery.length; i++) {
int number = ran.nextInt(31) + 1;//放入数组要考虑是否重复!
if (this.isExists(lottery, number)==false) {
lottery[i] = number;
} else {
i--;// 此次彩票号码在lottery彩票7个中已经存在
}
lottery[i] = ran.nextInt(31) + 1;// 放入数组要考虑是否重复!
}
return lottery;
}
/**
*
* 判断指定的彩票号number是否在彩票的7个号码中重复
*
* @param lottery 彩票7个号码,就是数组
* @param number 指定的彩票号
* @return true:已经存在,false:未存在
*/
public boolean isExists(int[] lottery, int number) {
for (int i = 0; i < lottery.length; i++) {
if (lottery[i] == number)
return true;
}
return false;
}
public static void main(String[] args) {
//本题很小,没有面向对象,使用面向过程,把每一步设计出来,统一在main方法里进行调用即可
Lottery l = new Lottery();
//1:随机产生一注系统中奖彩票
int[] syslottery = l.creatLottery();
//2:随机产生一注小明购买的彩票!
int[] customerlottery = l.creatLottery();
//3:进行比对 看相同号码个数
Lottery.queryResult(syslottery, customerlottery);
}
public static void queryResult(int[] syslottery,int[] customerlottery) {
System.out.println("中奖彩票是:"+Arrays.toString(syslottery));
System.out.println("小明彩票是:"+Arrays.toString(customerlottery));
int count = 0; //统计相同号码的个数!
wai:for (int i = 0; i < syslottery.length; i++) {
//遍历系统彩票
for (int j = 0; j < customerlottery.length; j++) {
//遍历客户彩票
if (syslottery[i]==customerlottery[j]) {
count++;
System.out.println("第"+count+"个中奖号码:"+syslottery[i]);
continue wai;
//每注彩票里的17个号码都是唯一的!
}
}
}
switch (count) {
case 7:
System.out.println("恭喜你!特等奖!");
break;
case 6:
System.out.println("恭喜你!1等奖!");
break;
case 5:
System.out.println("恭喜你!2等奖!");
break;
case 4:
System.out.println("恭喜你!3等奖!");
break;
case 3:
System.out.println("恭喜你!4等奖!");
break;
case 2:
System.out.println("恭喜你!5等奖!");
break;
case 1:
System.out.println("恭喜你!6等奖!");
break;
default:
System.out.println("很遗憾!没有相同号码!");
break;
}
}
}
解析写在了注释中,还请批评指正。
对我的博客或其他方面有任何见解或问题的话都可以私信我
或者联系本人QQ:3128909688
微信:DreamHerome
欢迎致电