Java实战入门之双色球彩票小游戏
一、项目需求
二、项目思路
1、菜单制作
2、中奖号码生成 getNumber (随机数 Math.random)
3、购买号码和中奖号码比对 生成中奖等级
package com.test.mxl; import java.util.Scanner; public class Test { public static void main(String[] args) { int[] balls = new int[7]; int count = 0; boolean isBuy = false;//默认情况下没有买彩票 while (true){ System.out.println("------------欢迎进入双色球彩票系统----------------"); System.out.println("1、购买彩票"); System.out.println("2、查看开奖"); System.out.println("3、退出"); System.out.println("请选择你要完成的功能"); Scanner sc = new Scanner(System.in); int choice = sc.nextInt(); switch (choice){ case 1: System.out.println("双色球系统》》购买彩票"); System.out.println("请选择你要购买几注:"); count = sc.nextInt(); //定义一个变量 如果每购买提示不能直接开奖 for (int i = 1; i <= 7; i++) { if(i != 7){//录入红色 System.out.println("请录入第"+i+"个红球"); balls[i-1] = sc.nextInt(); }else { //录入蓝色 System.out.println("请录入一个蓝色球"); balls[i-1] = sc.nextInt(); } } System.out.println("您购买了"+count+"注彩票,一共消费了"+count*2+"元钱,您购买的彩票号码为: "); //遍历数组 for (int num: balls) { System.out.print(num+"\t"); } System.out.println(); isBuy = true; break; case 2: if (isBuy){ System.out.println("双色球系统》》查看开奖"); //1 生成 int[] luckBalls= getNumber(); //2 对比 int level = getlevel(balls,luckBalls); //3 显示结果 System.out.println("本期中奖号码为:"+luckBalls[0]+" "+luckBalls[1]+" "+luckBalls[2]+" "+luckBalls[3]+" "+luckBalls[4]+" "+luckBalls[5]+" "+luckBalls[6]); switch (level){ case 1: System.out.println("恭喜您,中了一等奖,1注奖金500万,您一共获得: "+500*count+"万元"); break; case 2: System.out.println("恭喜您,中了二等奖,1注奖金100万,您一共获得: "+100*count+"万元"); break; case 3: System.out.println("恭喜您,中了三等奖,1注奖金30万,您一共获得: "+30*count+"万元"); break; case 4: System.out.println("恭喜您,中了四等奖,1注奖金5万,您一共获得: "+5*count+"万元"); break; case 5: System.out.println("恭喜您,中了五等奖,1注奖金2000千元,您一共获得: "+2000*count+"元"); break; case 6: System.out.println("恭喜您,中了六等奖,1注奖金5元,您一共获得: "+5*count+"元"); break; } }else { System.out.println("对不起,请先购买彩票"); } break; case 3: System.out.println("双色球系统》》退出"); return; } } } public static int[] getNumber(){ int[] getNumber = new int[7]; //随机数生成:红球 1-33,篮球1-16, Math.random() --[0.0,1.0) 可乘数; for (int i = 1; i <= 7; i++) { if(i != 7){ getNumber[i-1] = (int)(Math.random()*33 + 1); }else { getNumber[i-1] = (int)(Math.random()*16 + 1); } } return getNumber; } //比对是否中奖,中几等奖 public static int getlevel(int[] balls,int[] luckBalls){ //设置两个计数 红球和篮球 每相同一个就加一次。 int level = 0; int redCount = 0; int blueCount = 0; //遍历购买的号码 for (int i = 0; i < 7; i++) { if (i != 6){ for (int j = 0; j < 6; j++) { if (balls[i] == luckBalls[j]){ redCount++; } } }else { //篮球中奖 if(balls[6] == luckBalls[6]){ blueCount++; } } } //输出比对结果 //根据红蓝球相等数量得到level结果 if(redCount == 6 && blueCount == 1){ level = 1; } else if(redCount == 6 ){ level = 2; } else if(redCount == 5 && blueCount == 1){ level = 3; } else if(redCount == 5 || (redCount == 4 && blueCount==1)){ level = 4; } else if (redCount == 4 || (redCount == 3 && blueCount==1)){ level = 5; }else { level = 6; } return level; } }
三、功能演示
到此这篇关于Java实战入门之双色球彩票的文章就介绍到这了,更多相关java双色球彩票内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)