1. 程式人生 > >java雙色球

java雙色球

import java.util.Scanner;

public class shaungseqiu {

	public static void main(String[] args) {
		int totalMoney = 0;
		int price = 2;
		int count = 0;
		boolean isBuy = false;
		int num[] = null;
		do {// 做無限迴圈
			System.out.println("~~~~~~~~~~歡迎進入雙色球彩票系統~~~~~~~~~~~");

			System.out.println("\t1.購買彩票");
			System.out.println("\t2.檢視開獎");
			System.out.println("\t3.退出");
			System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
			System.out.println("請選擇:");
			Scanner sc = new Scanner(System.in);
			int chose = sc.nextInt();

			switch (chose) {
			case 1:
				//System.out.println("開始購買彩票");
				System.out.println("你要買多少注");
				count = sc.nextInt();// 買多少注
				totalMoney = count * price;// 下注需要的錢數
				// 開始選擇號碼6個紅球和1個籃球
				num = new int[7];
				for (int i = 0; i < num.length; i++) {
					int red;
					int blue;
					if (i < num.length - 1) {// 選紅色的號碼,要一個一個輸入,不能一起輸入
						System.out.println("請輸入6個紅色的號碼(數字為1-33),第"+(i+1)+"個紅色球號為");
						red = sc.nextInt(33);
						num[i] = red;
					} else {// 選藍色的號碼
						System.out.println("請輸入1個藍色的號碼(數字為1-16)");
						blue = sc.nextInt(16);
						num[i] = blue;
					}
				}
				// 輸出買的彩票資訊;
				System.out.println("你一共花了[" + count + "]一共要[" + totalMoney + "元,所選的號碼為:");
				for (int i = 0; i < num.length; i++) {
					System.out.print("你選的紅色號碼W為:");
					if(i<num.length-1){
						
						System.out.print(num[i]+",");
					}
					else System.out.print("你選的藍色號碼為"+num[i]);
				}
				System.out.println();
				isBuy = true;
				break;
			case 2:
				System.out.println("檢視開獎系統");
				if (isBuy) {
					isBuy = false;
					int luckNum[] = getLuckNum();
					for (int n : num) {
						System.out.println(n + "\t");
					}
					System.out.println();
					System.out.println("當期的中獎號碼為:");
					for (int n : luckNum) {
						System.out.print(n + ",");
					}
					System.out.println();
					int result = getCompareResult(num, luckNum);
					if (result == 1) {
						System.out.println("[一等獎],共下注[" + count + "],投資了[" + totalMoney + "]元,獲獎" + (500 * count) + "萬元");
					} else if (result == 2) {
						System.out.println("[二等獎],共下注[" + count + "],投資了[" + totalMoney + "]元,獲獎" + (400 * count) + "萬元");
					} else if (result == 3) {
						System.out.println("[三等獎],共下注[" + count + "],投資了[" + totalMoney + "]元,獲獎" + (300 * count) + "元");
					} else if (result == 4) {
						System.out.println("[四等獎],共下注[" + count + "],投資了[" + totalMoney + "]元,獲獎" + (200 * count) + "元");
					} else if (result == 5) {
						System.out.println("[五等獎],共下注[" + count + "],投資了[" + totalMoney + "]元,獲獎" + (100 * count) + "元");
					} else if (result == 6) {
						System.out.println("[六等獎],共下注[" + count + "],投資了[" + totalMoney + "]元,獲獎" + (5 * count) + "元");
					} else {
						System.out.println("哈哈哈 ,你沒得獎,共下注[" + count + "],投資了[" + totalMoney + "]元");
					}
				} else {
					System.out.println("請先購買彩票");
				}
				break;
			case 3:
				System.out.println("謝謝使用");
				return;
			default:
				System.out.println("輸入錯誤");
			}

		} while (true);
	}

	// 生成一個隨機的中獎號碼
	public static int[] getLuckNum() {
		int luckNum[] = new int[7];
		for (int i = 0; i < luckNum.length; i++) {
			if (i < luckNum.length - 1) {
				luckNum[i] = (int) (Math.random() * 33) + 1;
			} else {
				luckNum[i] = (int) (Math.random() * 16) + 1;
			}
		}
		return luckNum;
	}

	// 計算 獲獎的等級 所買的彩票 當期中獎的號碼
	public static int getCompareResult(int num[], int luckNum[]) {
		// 檢視中獎的號碼
		int luckLevel = 0;
		int redEqualCount = 0;
		int blueEqualCount = 0;
		for (int i = 0; i < num.length; i++) {//買的號碼與中獎的號碼做對比
			if (i < num.length - 1) {//遍歷自己買號碼
				int r = num[i];
				for (int j = 0; j < luckNum.length - 1; j++) {
					if (r == luckNum[j])
						redEqualCount++;
				}
			} else {
				if (num[num.length - 1] == luckNum[luckNum.length-1])
					blueEqualCount++;
			}
		}
		// 判斷中獎的等級(根據中獎規則)
		if (redEqualCount == 6 && blueEqualCount == 1)
			luckLevel = 1;
		else if (redEqualCount == 6)
			luckLevel = 2;
		else if (redEqualCount == 5 && blueEqualCount == 1)
			luckLevel = 3;
		else if ((redEqualCount == 5) || (redEqualCount == 4 && blueEqualCount == 1))
			luckLevel = 4;
		else if ((redEqualCount == 5) || (redEqualCount == 3 && blueEqualCount == 1))
			luckLevel = 5;
		else if (blueEqualCount == 1)
			luckLevel = 6;
		return luckLevel ;//注意返回的值是luckLevel 
	}
}

專案反思:重點在於邏輯的思考;對業務要進行了解,其次要熟練陣列,迴圈,判斷,方法的使用.有機會把專案擴充套件一下

雙色球2;

import java.util.Arrays;
import java.util.Scanner;

public class DoubleBall {
	
	
	
	//雙色球的陣列  自選的雙色球陣列
	 static int[] dbBall = new int[6];
	 static int blueB = 0;
	 //正確的雙色球陣列
	 static int[] cdbBall = new int[6];
	 static int cblueB = 0;
	 //下注數
	 static int n = 0;
	 //單注獎金
	 static int money = 5000000;
	/**
	 * 彩票主頁
	 */
	public static void index(){
		System.out.println("------尚學堂彩票站-------");
		System.out.println("      1:買彩票");
		System.out.println("      2:開獎");
		System.out.println("      3:退出");
		System.out.println("----------------------");
		
		Scanner sc = new Scanner(System.in);
		System.out.println("請選擇[1-3]");
		int num = sc.nextInt();
		switch(num){
			case 1:buyBall();break;
			case 2:lottery();break;
			case 3:System.out.println("歡迎下次光臨");return;
			default:System.out.println("請重新輸入");index();
		}
		
		
	}
	
	
	
	/**
	 * 買彩票
	 * @param args
	 */
	
	public static void buyBall(){
		
		
		Scanner sc = new Scanner(System.in);
		System.out.println("請下注  1注2元"); 
		//下注的數字
		n = sc.nextInt();
		
		System.out.println("1:機選 2:手選");
		int num = sc.nextInt();
		if(num==1){
			buyBall1(); //機選
		}else if(num==2){
			buyBall2(dbBall); //手選
		}else{
			System.out.println("數字有誤");
			buyBall();
		}	
	}
	/**
	 * 機選
	 */
	public static void buyBall1() {
       	
		//紅球的範圍1-33 不能重複  總共6個
		    int count = 0;
			for(int i=0;;i++){
				boolean flag = true;//狀態值,初始值為true
				int red = (int)(Math.random()*33)+1;
				for(int j = 0;j<dbBall.length;j++){
					//開始判斷是否有重複的球
					if(red==dbBall[j]){
						flag=false; //如果有重複值,flag變為false
						break;
					}
					
				}
				//判斷隨機數是否有重複,flag為true代表沒有重複
				if(flag){
					dbBall[count++] = red;
				}
				//當紅球全都放入陣列後,跳出大的迴圈
				if(dbBall[5]!=0){
					break;
				}
			}
		
		
		//藍球的範圍1-16   總共1個
		blueB =  (int)(Math.random()*16)+1;
		Arrays.sort(dbBall); //陣列排序
		System.out.println("機選號碼:"+Arrays.toString(dbBall)+"["+blueB+"]");
		index();//返回主介面
	}
	/**
	 * 手選
	 */
	public static void buyBall2(int[] dbBall) {
        		
	}






	/**
	 * 開獎
	 * @param args
	 */
	public static  void lottery(){
		//機器生成正確的彩票號碼
	    int count = 0;
		for(int i=0;;i++){
			boolean flag = true;//狀態值,初始值為true
			int red = (int)(Math.random()*33)+1;
			for(int j = 0;j<cdbBall.length;j++){
				//開始判斷是否有重複的球
				if(red==cdbBall[j]){
					flag=false; //如果有重複值,flag變為false
					break;
				}
				
			}
			//判斷隨機數是否有重複,flag為true代表沒有重複
			if(flag){
				cdbBall[count++] = red;
			}
			//當紅球全都放入陣列後,跳出大的迴圈
			if(cdbBall[5]!=0){
				break;
			}
		}
	
	
	//藍球的範圍1-16   總共1個
	cblueB =  (int)(Math.random()*16)+1;
	Arrays.sort(cdbBall); //陣列排序
	System.out.println("開獎號碼:"+Arrays.toString(cdbBall)+"["+cblueB+"]");
	winningPrize();
	}
	
	/**
	 * 中獎功能
	 */
	public static void winningPrize(){
		int count = 0;
		for(int i=0;i<dbBall.length;i++){ //機選號碼
			for(int j=0;j<cdbBall.length;j++){
				if(dbBall[i]==cdbBall[j]){
					count++; //紅球正確個數的計數器
					break;
				}
				
			}
			
		}
		
		//藍球  的判斷
		boolean blue = (blueB == cblueB);
		if(count==6&&blue){
			System.out.println("一等獎:"+(n*money));
		}else if(count==6){
			System.out.println("二等獎:"+(n*money*0.25));
		}else if(count==5&&blue){
			System.out.println("三等獎:"+(n*3000));
		}else if(count==5||(count==4&&blue)){
			System.out.println("四等獎:"+(n*200));
		}else if(count==4||(count==3&&blue)){
			System.out.println("五等獎:"+(n*10));
		}else if(blue){
			System.out.println("六等獎:"+(n*5));
		}else{
			System.out.println("未中獎,謝謝惠顧");
		}
		
		index();
	}

	
	public static void main(String[] args) {
		index();
	}
	
	

}