java实现双色球彩票游戏
综合案例开发:模拟双色球彩票游戏
玩法说明:
双色球投注区分为红球号码区和蓝球号码区,红球号码范围为01~33,蓝球号码范围为01~16。双色球每期从33 个红球中开出6个号码(不能重复),从16个蓝球中开出1个号码作为中奖号码,双色球玩法即是竞猜开奖号码的6 个红球号码和1个蓝球号码,顺序不限。 用户输入红球和蓝球号码,程序输出该用户中几等奖。
代码实现:
import java.util.Random; import java.util.Scanner; public class SimulatedLottery { public static void main(String[] args) { //单注最高奖金 int maxMoney = 500; //输入蓝球的号码 System.out.print("请输入你购买的蓝球号码:"); Scanner input = new Scanner(System.in); int blueBall = input.nextInt(); //输入红球的数组 int[] redBall = new int[6]; System.out.print("请输入你购买的红球号码(不重复):"); for (int i = 0; i < redBall.length; i++) { redBall[i] = input.nextInt(); } //输出输入值 System.out.println("----------------"); System.out.print("你购买的红球号码是:"); for (int i = 0; i < redBall.length; i++) { System.out.print(redBall[i]+","); } System.out.println(); System.out.println("你购买的蓝球号码是:"+blueBall); System.out.println("---正在产生中奖号码---"); //生成的蓝球号码 Random numsRandom = new Random(); int blueBallRandom = numsRandom.nextInt(16)+1; //生成红球的号码 int[] redBallRandom = new int[6]; int index = redBallRandom.length; int inputRandom = 0; int k = 0; while (index>0) { if (exist(redBallRandom, inputRandom)) { //在数组中存在,更换一个随机数 inputRandom = numsRandom.nextInt(33)+1; }else { //在数组中不存在 redBallRandom[k] = inputRandom; k++; index--; } } //输出中奖号码 System.out.println("蓝球的中奖号码是:"+blueBallRandom); System.out.print("红球的中奖号码是:"); for (int i = 0; i < redBallRandom.length; i++) { System.out.print(redBallRandom[i]+","); } System.out.println(); //统计和蓝球相等的个数 int blueCount = 0; if (blueBall == blueBallRandom) { blueCount = 1; } //统计和红球相等的个数 int redCount = 0; for (int i = 0; i < redBallRandom.length; i++) { if (redBall[i] == redBallRandom[i]) { redCount++; } } //判断是否中奖 if (blueCount == 0 && redCount <= 3) { //未中奖 System.out.println("很遗憾,您未中奖,赌博害人,请勿上头!"); //中奖 }else if(blueCount == 1 && redCount < 3) { System.out.println("恭喜你,中了六等奖,您的奖金为5元"); }else if((blueCount == 1 && redCount == 3) || (blueCount == 0 && redCount == 4)) { System.out.println("恭喜你,中了五等奖,您的奖金为10元"); }else if((blueCount == 1 && redCount == 4) && (blueCount == 0 && redCount == 5)) { System.out.println("恭喜你,中了四等奖,您的奖金为200元"); }else if(blueCount == 1 && redCount == 5) { System.out.println("恭喜你,中了三等奖,您的奖金为3000元"); }else if(blueCount == 0 && redCount == 6) { System.out.println("恭喜你,中了二等奖,您的奖金为"+(int)(maxMoney*0.3)+"万"); }else if(blueCount == 1 && redCount == 6 ) { System.out.println("恭喜你,中了一等奖,您的奖金为"+maxMoney+"万"); } } //判断数组中是否存在某数的方法,存在返回true public static boolean exist(int[] redBallRandom, int inputRandom) { for (int i = 0; i < redBallRandom.length; i++) { if(redBallRandom[i] == inputRandom) { return true; } } return false; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)