7.10 作業 韓宗軒
阿新 • • 發佈:2018-07-10
ring 雙色球彩票 static oid dom equal ext can port
1 import java.util.Scanner; 2 class SssGame { 3 public static void main(String[] args) { 4 int totalMoney = 0; 5 int price = 2; 6 int count = 0; 7 boolean isBuy = false; 8 int num[] = null; 9 do{ 10 System.out.println("******歡迎進入雙色球彩票系統******"); 11 System.out.println("\t 1.購買彩票");12 System.out.println("\t 2.查看開獎"); 13 System.out.println("\t 3.退出"); 14 System.out.println("*********************************"); 15 System.out.println("請選擇菜單"); 16 17 Scanner input = new Scanner(System.in); 18 int choice = input.nextInt(); 19 switch (choice) { 20case 1: 21 System.out.println("[雙色球系統--->購買彩票]"); 22 System.out.println("您需要下多少註? : "); 23 count = input.nextInt(); 24 totalMoney = count * price; 25 26 num = new int[7]; 27 for (int i = 0; i < num.length;i++ ) { 28 intred; 29 int blue; 30 if (i < num.length - 1) { 31 System.out.println("請輸入6個紅色球號(數字為1-33),第" + (i + 1 )+"個紅色球號碼為:"); 32 red = input.nextInt(); 33 num[i] = red; 34 35 }else{ 36 System.out.println("請輸入1個藍色球號,數字為(1-16):"); 37 blue = input.nextInt(); 38 num[i] = blue; 39 } 40 } 41 System.out.print("您一共買了"+ count + "註,共需支付"+totalMoney+"元,所選號為:"); 42 for (int i = 0;i < num.length ;i++ ) { 43 System.out.print(num[i]+"\t"); 44 } 45 46 System.out.println(); 47 isBuy = true; 48 break; 49 case 2: 50 System.out.println("[雙色球彩票系統--->查看開獎]"); 51 isBuy = false; 52 int luckNum[] = getLuckNum(); 53 System.out.print("已買的彩票號為: "); 54 for (int n:num ) { 55 System.out.print(n+"\t"); 56 57 } 58 System.out.println(); 59 int result = getCompareResult(num,luckNum); 60 if (result == 1) { 61 System.out.println("一等獎,恭喜您!共下註了"+ count+"註,投資"+totalMoney+"元,獲獎"+(500*count)+"萬元"); 62 63 }else if (result==2) { 64 System.out.println("二等獎,恭喜您!共下註了"+ count+"註,投資"+totalMoney+"元,獲獎"+(400*count)+"萬元"); 65 } 66 else if (result==3) { 67 System.out.println("三等獎,恭喜您!共下註了"+ count+"註,投資"+totalMoney+"元,獲獎"+(300*count)+"萬元"); 68 } 69 else if (result==4) { 70 System.out.println("四等獎,恭喜您!共下註了"+ count+"註,投資"+totalMoney+"元,獲獎"+(200*count)+"萬元"); 71 } 72 else if (result==5) { 73 System.out.println("五等獎,恭喜您!共下註了"+ count+"註,投資"+totalMoney+"元,獲獎"+(100*count)+"萬元"); 74 } 75 else if (result==6) { 76 System.out.println("六等獎,恭喜您!共下註了"+ count+"註,投資"+totalMoney+"元,獲獎"+(5*count)+"萬元"); 77 }else{ 78 System.out.println("望再接再勵,sorry!!共下註了"+ count+"註,投資"+totalMoney+"元,獲獎"+(0*count)+"萬元"); 79 } 80 81 break; 82 case 3: 83 System.out.println("謝謝使用"); 84 return; 85 default: 86 System.out.println("輸入有誤"); 87 break; 88 } 89 }while(true); 90 91 } 92 public static int[] getLuckNum(){ 93 int luckNum [] = new int[7]; 94 for (int i = 0;i< luckNum.length ;i++ ) { 95 if (i<luckNum.length-1) { 96 luckNum[i] = (int)(Math.random()*33 + 1); 97 }else{ 98 luckNum[i] = (int)(Math.random()*16+1); 99 } 100 } 101 return luckNum; 102 } 103 public static int getCompareResult(int num [],int luckNum[]){ 104 int luckLevel = 0; 105 int redEqualCount = 0; 106 int blueEqualCount = 0; 107 for (int i = 0;i<num.length ;i++ ) { 108 if (i<num.length - 1) { 109 int r = num[i]; 110 for (int j = 0;j<luckNum.length - 1 ;j++ ) { 111 if (r==luckNum[j]) { 112 redEqualCount++; 113 } 114 } 115 }else{ 116 if ((num[num.length-1])==luckNum[luckNum.length-1]) { 117 blueEqualCount++; 118 } 119 } 120 } 121 if (redEqualCount==6&&blueEqualCount==1) { 122 luckLevel=1; 123 }else if (redEqualCount==6) { 124 luckLevel=2; 125 } 126 else if (redEqualCount==5&&blueEqualCount==1) { 127 luckLevel=3; 128 } 129 else if (redEqualCount==5||(redEqualCount==4&&blueEqualCount==1)) { 130 luckLevel=4; 131 } 132 else if (redEqualCount==4||(redEqualCount==4&&blueEqualCount==1)) { 133 luckLevel=5; 134 } 135 else if (blueEqualCount==1) { 136 luckLevel=2; 137 } 138 else if (redEqualCount==6) { 139 luckLevel=6; 140 } 141 return luckLevel; 142 } 143 }
7.10 作業 韓宗軒