1. 程式人生 > 程式設計 >java實現雙色球彩票遊戲

java實現雙色球彩票遊戲

綜合案例開發:模擬雙色球彩票遊戲

玩法說明:

雙色球投注區分為紅球號碼區和藍球號碼區,紅球號碼範圍為01~33,藍球號碼範圍為01~16。雙色球每期從33 個紅球中開出6個號碼(不能重複),從16個藍球中開出1個號碼作為中獎號碼,雙色球玩法即是競猜開獎號碼的6 個紅球號碼和1個藍球號碼,順序不限。 使用者輸入紅球和藍球號碼,程式輸出該使用者中幾等獎。

java實現雙色球彩票遊戲

程式碼實現:

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;
 
 }

}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。