java實現(xiàn)雙色球彩票游戲
綜合案例開發(fā):模擬雙色球彩票游戲,供大家參考,具體內(nèi)容如下
玩法說明:
雙色球投注區(qū)分為紅球號碼區(qū)和藍球號碼區(qū),紅球號碼范圍為01~33,藍球號碼范圍為01~16。雙色球每期從33 個紅球中開出6個號碼(不能重復(fù)),從16個藍球中開出1個號碼作為中獎號碼,雙色球玩法即是競猜開獎號碼的6 個紅球號碼和1個藍球號碼,順序不限。 用戶輸入紅球和藍球號碼,程序輸出該用戶中幾等獎。
代碼實現(xiàn):
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(); //輸入紅球的數(shù)組 int[] redBall = new int[6]; System.out.print("請輸入你購買的紅球號碼(不重復(fù)):"); 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("---正在產(chǎn)生中獎號碼---"); //生成的藍球號碼 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)) { //在數(shù)組中存在,更換一個隨機數(shù) inputRandom = numsRandom.nextInt(33)+1; }else { //在數(shù)組中不存在 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(); //統(tǒng)計和藍球相等的個數(shù) int blueCount = 0; if (blueBall == blueBallRandom) { blueCount = 1; } //統(tǒng)計和紅球相等的個數(shù) 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+"萬"); } } //判斷數(shù)組中是否存在某數(shù)的方法,存在返回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; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于@RequestBody和@ResponseBody及Stringify()的作用說明
這篇文章主要介紹了基于@RequestBody和@ResponseBody及Stringify()的作用說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06Spring?AOP操作的相關(guān)術(shù)語及環(huán)境準備
這篇文章主要為大家介紹了Spring?AOP操作的相關(guān)術(shù)語及環(huán)境準備學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05Java SMM框架關(guān)聯(lián)關(guān)系映射示例講解
SSM框架是spring MVC ,spring和mybatis框架的整合,是標準的MVC模式,將整個系統(tǒng)劃分為表現(xiàn)層,controller層,service層,DAO層四層,使用spring MVC負責(zé)請求的轉(zhuǎn)發(fā)和視圖管理,spring實現(xiàn)業(yè)務(wù)對象管理,mybatis作為數(shù)據(jù)對象的持久化引擎2022-08-08Mybatis-Plus自動填充更新操作相關(guān)字段的實現(xiàn)
這篇文章主要介紹了Mybatis-Plus自動填充更新操作相關(guān)字段的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12spring一個項目多個模塊聚合打包問題解決方案(最新推薦)
最近遇到個需求,針對后端解耦模塊較多的項目,想在云端啟動時簡潔些只啟動一個jar文件的情景,本文重點給大家介紹spring一個項目多個模塊聚合打包問題解決方案,感興趣的朋友一起看看吧2023-09-09Java Builder模式構(gòu)建MAP/LIST的實例講解
下面小編就為大家?guī)硪黄狫ava Builder模式構(gòu)建MAP/LIST的實例講解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10SpringBoot項目中連接SQL Server的三種方式
連接SQL Server是許多Spring Boot項目中常見的需求之一,本文主要介紹了SpringBoot項目中連接SQL Server的三種方式,具有一定的參考價值 ,感興趣的可以了解一下2023-09-09使用注解+RequestBodyAdvice實現(xiàn)http請求內(nèi)容加解密方式
這篇文章主要介紹了使用注解+RequestBodyAdvice實現(xiàn)http請求內(nèi)容加解密方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06