java實(shí)現(xiàn)雙色球彩票游戲
綜合案例開(kāi)發(fā):模擬雙色球彩票游戲,供大家參考,具體內(nèi)容如下
玩法說(shuō)明:
雙色球投注區(qū)分為紅球號(hào)碼區(qū)和藍(lán)球號(hào)碼區(qū),紅球號(hào)碼范圍為01~33,藍(lán)球號(hào)碼范圍為01~16。雙色球每期從33 個(gè)紅球中開(kāi)出6個(gè)號(hào)碼(不能重復(fù)),從16個(gè)藍(lán)球中開(kāi)出1個(gè)號(hào)碼作為中獎(jiǎng)號(hào)碼,雙色球玩法即是競(jìng)猜開(kāi)獎(jiǎng)號(hào)碼的6 個(gè)紅球號(hào)碼和1個(gè)藍(lán)球號(hào)碼,順序不限。 用戶輸入紅球和藍(lán)球號(hào)碼,程序輸出該用戶中幾等獎(jiǎng)。

代碼實(shí)現(xiàn):
import java.util.Random;
import java.util.Scanner;
public class SimulatedLottery {
public static void main(String[] args) {
//單注最高獎(jiǎng)金
int maxMoney = 500;
//輸入藍(lán)球的號(hào)碼
System.out.print("請(qǐng)輸入你購(gòu)買(mǎi)的藍(lán)球號(hào)碼:");
Scanner input = new Scanner(System.in);
int blueBall = input.nextInt();
//輸入紅球的數(shù)組
int[] redBall = new int[6];
System.out.print("請(qǐng)輸入你購(gòu)買(mǎi)的紅球號(hào)碼(不重復(fù)):");
for (int i = 0; i < redBall.length; i++) {
redBall[i] = input.nextInt();
}
//輸出輸入值
System.out.println("----------------");
System.out.print("你購(gòu)買(mǎi)的紅球號(hào)碼是:");
for (int i = 0; i < redBall.length; i++) {
System.out.print(redBall[i]+",");
}
System.out.println();
System.out.println("你購(gòu)買(mǎi)的藍(lán)球號(hào)碼是:"+blueBall);
System.out.println("---正在產(chǎn)生中獎(jiǎng)號(hào)碼---");
//生成的藍(lán)球號(hào)碼
Random numsRandom = new Random();
int blueBallRandom = numsRandom.nextInt(16)+1;
//生成紅球的號(hào)碼
int[] redBallRandom = new int[6];
int index = redBallRandom.length;
int inputRandom = 0;
int k = 0;
while (index>0) {
if (exist(redBallRandom, inputRandom)) {
//在數(shù)組中存在,更換一個(gè)隨機(jī)數(shù)
inputRandom = numsRandom.nextInt(33)+1;
}else {
//在數(shù)組中不存在
redBallRandom[k] = inputRandom;
k++;
index--;
}
}
//輸出中獎(jiǎng)號(hào)碼
System.out.println("藍(lán)球的中獎(jiǎng)號(hào)碼是:"+blueBallRandom);
System.out.print("紅球的中獎(jiǎng)號(hào)碼是:");
for (int i = 0; i < redBallRandom.length; i++) {
System.out.print(redBallRandom[i]+",");
}
System.out.println();
//統(tǒng)計(jì)和藍(lán)球相等的個(gè)數(shù)
int blueCount = 0;
if (blueBall == blueBallRandom) {
blueCount = 1;
}
//統(tǒng)計(jì)和紅球相等的個(gè)數(shù)
int redCount = 0;
for (int i = 0; i < redBallRandom.length; i++) {
if (redBall[i] == redBallRandom[i]) {
redCount++;
}
}
//判斷是否中獎(jiǎng)
if (blueCount == 0 && redCount <= 3) {
//未中獎(jiǎng)
System.out.println("很遺憾,您未中獎(jiǎng),賭博害人,請(qǐng)勿上頭!");
//中獎(jiǎng)
}else if(blueCount == 1 && redCount < 3) {
System.out.println("恭喜你,中了六等獎(jiǎng),您的獎(jiǎng)金為5元");
}else if((blueCount == 1 && redCount == 3) || (blueCount == 0 && redCount == 4)) {
System.out.println("恭喜你,中了五等獎(jiǎng),您的獎(jiǎng)金為10元");
}else if((blueCount == 1 && redCount == 4) && (blueCount == 0 && redCount == 5)) {
System.out.println("恭喜你,中了四等獎(jiǎng),您的獎(jiǎng)金為200元");
}else if(blueCount == 1 && redCount == 5) {
System.out.println("恭喜你,中了三等獎(jiǎng),您的獎(jiǎng)金為3000元");
}else if(blueCount == 0 && redCount == 6) {
System.out.println("恭喜你,中了二等獎(jiǎng),您的獎(jiǎng)金為"+(int)(maxMoney*0.3)+"萬(wàn)");
}else if(blueCount == 1 && redCount == 6 ) {
System.out.println("恭喜你,中了一等獎(jiǎng),您的獎(jiǎng)金為"+maxMoney+"萬(wàn)");
}
}
//判斷數(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;
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java多線程編程之從線程返回?cái)?shù)據(jù)的兩種方法
從線程中返回?cái)?shù)據(jù)和向線程傳遞數(shù)據(jù)類似。也可以通過(guò)類成員以及回調(diào)函數(shù)來(lái)返回?cái)?shù)據(jù)。但類成員在返回?cái)?shù)據(jù)和傳遞數(shù)據(jù)時(shí)有一些區(qū)別,下面讓我們來(lái)看看它們區(qū)別在哪2014-01-01
Java list利用遍歷進(jìn)行刪除操作3種方法解析
這篇文章主要介紹了Java list利用遍歷進(jìn)行刪除操作3種方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
解決mybatisplus插入報(bào)錯(cuò)argument type mismatch的問(wèn)題
這篇文章主要介紹了解決mybatisplus插入報(bào)錯(cuò)argument type mismatch的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
解讀@NoArgsConstructor,@AllArgsConstructor,@RequiredArgsConstr
這篇文章主要介紹了解讀@NoArgsConstructor,@AllArgsConstructor,@RequiredArgsConstructor的區(qū)別及在springboot常用地方,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
SpringBoot+MyBatis-Plus實(shí)現(xiàn)分頁(yè)的項(xiàng)目實(shí)踐
MyBatis-Plus是基于MyBatis的持久層增強(qiáng)工具,提供簡(jiǎn)化CRUD、代碼生成器、條件構(gòu)造器、分頁(yè)及樂(lè)觀鎖等功能,極大簡(jiǎn)化了開(kāi)發(fā)工作量并提高了開(kāi)發(fā)效率,本文就來(lái)介紹一下SpringBoot+MyBatis-Plus實(shí)現(xiàn)分頁(yè)的項(xiàng)目實(shí)踐,感興趣的可以了解一下2024-11-11
Spring Boot集成Seata實(shí)現(xiàn)基于AT模式的分布式事務(wù)的解決方案
Seata 是一款開(kāi)源的分布式事務(wù)解決方案,致力于提供高性能和簡(jiǎn)單易用的分布式事務(wù)服務(wù),這篇文章主要介紹了Spring Boot集成Seata實(shí)現(xiàn)基于AT模式的分布式事務(wù),需要的朋友可以參考下2024-08-08

