运用JAVA基础阶段知识,模拟双色球机选抽奖过程
在控制台输入抽奖次数,进行双色球抽奖,并统计中奖结果
//双色球(机选)
//红球33个选6个,蓝球16个选1个
//中奖号码设定为1,2,3,4,5,6,7
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class ChouJiang {
public static void main(String[] args) {
//输入想购买的次数
Scanner s1 = new Scanner(System.in);
System.out.println("请输入您要购买的彩票数!");
int times=s1.nextInt();
int count=0;
int count1=0;
int count2=0;
int count3=0;
int count4=0;
int count5=0;
int count6=0;
//奖池号码
int[] num={1,2,3,4,5,6,7};
for (int k = 0; k < times; k++) {
//产生序号不重复的6个红球
Random redball = new Random();
int[] redArr = new int[6];
//循环产生随机数 存入redArr数组
for (int i = 0; i < 6 ; i++) {
int red=redball.nextInt(33)+1;
boolean a=true;
//判断这个数字是否与数组内原先存入的数字重复
for (int j = 0; j < 6; j++) {
if(red==redArr[j]) {
a=false;
}
}
if(a){
redArr[i]=(red);
} else {//重复则i-1,重新循环一次
i--;
}
}
System.out.print("红球:"+Arrays.toString(redArr));
//产生一个蓝球
Random buleball = new Random();
int blue=buleball.nextInt(16)+1;
System.out.println(" "+"蓝球:"+"["+blue+"]");
int countRed=0;
int countBule=0;
//中奖规则
//六等奖:2+1或1+1或0+1
//五等奖:4+0或3+1
//四等奖:5+0或4+1
//三等奖:5+1
//二等奖:6+0
//一等奖:6+1
for (int i = 0; i < 6; i++) {
for (int j = 0; j <6 ; j++) {
if(redArr[i]==num[j]){
countRed++;
}
}
}
if(num[6]==blue){
countBule++;
}
//输出中奖结果
if(countRed<3&countBule==0){
System.out.println("没中奖");
}else if((countRed<3)&(countBule==1)){
System.out.println("六等奖");
count6++;
}else if(countBule+countRed==4){
System.out.println("五等奖");
count5++;
}else if(countBule+countRed==5){
System.out.println("四等奖");
count4++;
}else if(countRed==5&countBule==1){
System.out.println("三等奖");
count3++;
}else if(countRed==6&countBule==0){
System.out.println("二等奖");
count2++;
}else if(countRed==6&countBule==1){
System.out.println("一等奖");
count1++;
}
count++;
}
System.out.println("总抽奖次数:"+count);
System.out.println("六等奖次数:"+count6);
System.out.println("五等奖次数:"+count5);
System.out.println("四等奖次数:"+count4);
System.out.println("三等奖次数:"+count3);
System.out.println("二等奖次数:"+count2);
System.out.println("一等奖次数:"+count1);
}
}