项目需求
买彩票判断是否中奖,中奖后判断是几等奖
双色球投注区分为红色球号码区和蓝色球号码区,
红色球号码区由1-33共33个号码组成,蓝色球号码区由1-16共16个号码组成。
投注时选择6个红球号码和1个蓝球号码组成一注进行单式投注,每注金额2元。
一等奖:投注号码与当期开奖号码全部相同(顺序不限,下同),即中奖;
二等奖:投注号码与当期开奖号码中的6个红色球号码相同,即中奖;
三等奖:投注号码与当期开奖号码中的任意5个红色球号码和1个蓝色球号码相同,即中奖;
四等奖:投注号码与当期开奖号码中的任意5个红色球号码相同,或与任意4个红色球号码和1个蓝色球号码相同,即中奖;
五等奖:投注号码与当期开奖号码中的任意4个红色球号码相同,或与任意3个红色球号码和1个蓝色球号码相同,即中奖;
六等奖:投注号码与当期开奖号码中的1个蓝色球号码相同,即中奖。
项目代码
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
static Long bet;
public static void main(String[] args) {
boolean flag = false;
int[] arrbuy = new int[7];
while(true){
System.out.println("——————————欢迎购买双色球——————————");
System.out.println("1.购买双色球");
System.out.println("2.查看开奖");
System.out.println("3.退出");
System.out.print("请选择:");
boolean sixprize = false;
int num = sc.nextInt();
switch(num){
case 1:
flag = true;
System.out.print("请选择下注数(每注100元):");
bet = sc.nextLong();
for(int i=0;i<6;i++){
int a = i+1;
System.out.println("请选择第"+a+"个红球的号码,(1~33之间)");
arrbuy[i] = sc.nextInt();
if(arrbuy[i]<1||arrbuy[i]>33){
System.out.println("您输入的数字异常!");
return;
}
if(i>0){
for(int j = i-1;j>=0;j--){
if(arrbuy[i]==arrbuy[j]){
System.out.println("您输入的数字异常!");
return;
}
}
}
}
System.out.println("请选择一个蓝球的号码,(1~16之间)");
arrbuy[6] = sc.nextInt();
break;
case 2:
int[] arrwin = new int[]{(int)(Math.random()*33+1),(int)(Math.random()*33+1),(int)(Math.random()*33+1),(int)(Math.random()*33+1),(int)(Math.random()*33+1),(int)(Math.random()*33+1),(int)(Math.random()*16+1)};
System.out.println("本期中奖号码是:");
for(int i=0;i<7;i++){
System.out.print(arrwin[i]+" ");
}
System.out.println();
int redball = 0;
int blueball = 0;
if(arrbuy[6]==arrwin[6]){
blueball = 1;
}
for(int i = 0;i<6;i++) {
if (arrbuy[i] == arrwin[6]) {
sixprize = true;
}
for (int j = 0; j < 6; j++) {
if (arrbuy[i] == arrwin[j]) {
redball++;
}
}
}
if(flag){
flag = false;
if(redball==6&&blueball==1){
System.out.println("恭喜您中了一等奖!");
System.out.println("奖金:1000万");
bet();
System.out.println("你的中奖金额为"+bet*1000+"万");
}else if(redball==6){
System.out.println("恭喜您中了二等奖!");
System.out.println("奖金:300万");
bet();
System.out.println("你的中奖金额为"+bet*300+"万");
}else if(redball==5&&blueball==1){
System.out.println("恭喜您中了三等奖!");
System.out.println("奖金:100万");
bet();
System.out.println("你的中奖金额为"+bet*100+"万");
}else if(redball==5||(redball==4&&blueball==1)){
System.out.println("恭喜您中了四等奖!");
System.out.println("奖金:10万");
bet();
System.out.println("你的中奖金额为"+bet*10+"万");
}else if(redball==4||(redball==3&&blueball==1)){
System.out.println("恭喜您中了五等奖!");
System.out.println("奖金:3万");
bet();
System.out.println("你的中奖金额为"+bet*3+"万");
}else if(sixprize){
System.out.println("恭喜您中了六等奖!");
System.out.println("奖金:1万");
bet();
System.out.println("你的中奖金额为"+bet*1+"万");
}
else{
System.out.println("抱歉,您没有中奖");
}
back();
}else{
System.out.println("您还没有购买彩票!");
back();
}
break;
case 3:
System.out.println("系统退出");
return;
}
}
}
public static void back(){
System.out.print("输入任意数字返回菜单:");
sc.nextInt();
}
public static void bet(){
System.out.println("您的下注数为"+bet+",下注金额为"+bet*100);
}
}
我的注意
1.static的使用
2.boolean作为"开关"的使用
3.数组的创建方法
4.Math.random()的使用:
Math.random的范围为[0.0,1.0)
两边同乘b-a+1,得[0.0,b-a+1)
强转int,得[0,b-a]
两边同时加a
需要范围为[a,b]的区间的随机数
5.引入“计数器”
6.一样的地方总结成方法
7.完善系统