【Java】實現雙色球隨機彩票


 

目前對於對於輸入沒有進行限制(可以輸入相同的數字或大於雙色球號碼的數字,同樣也可以輸入字符),之后會進行更新。

  

 

 1 package pres.work;
 2 
 3 import java.util.Arrays;
 4 import java.util.Random;
 5 import java.util.Scanner;
 6 
 7 /**
 8  * 實現雙色球隨機彩票
 9  * @author three
10  *
11  */
12 public class DoubleBall {
13 
14     public static void main(String[] args) {
15         
16         /**
17          * 隨機雙色球,紅球六個(不重復),藍球一個
18          */
19         int[] randomRedBall = new int[6];
20         int[] randomBlueBall = new int[1];
21         Random rand = new Random();
22         for(int i = 0; i<randomRedBall.length; i++) {
23             int r= rand.nextInt(33)+1;
24             randomRedBall[i] = r;
25             //遍歷,判斷是否重復
26             for(int j = 0;j<i;j++) {
27                 if(randomRedBall[j] == r) {
28                     --i;
29                     continue;
30                 }
31             }
32         }
33         randomBlueBall[0] = rand.nextInt(16)+1;
34 
35         /**
36          * 鍵盤輸入紅/藍球號碼 相鄰號碼以空格隔開
37          */
38         Scanner sc = new Scanner(System.in);
39         int[] setRedBall = new int[6]; 
40         System.out.print("請輸入選擇的紅球:");
41         for(int i = 0; i<randomRedBall.length; i++) {
42             setRedBall[i] = sc.nextInt();
43         }
44         System.out.print("請輸入選擇的藍球:");
45         int[] setBlueBall = new int[1];
46         setBlueBall[0] = sc.nextInt();
47         sc.close();
48         
49         //鍵盤輸入結束后,打印出中獎號碼,方便用戶對比
50         System.out.printf("中獎紅球號碼 %s\n",Arrays.toString(randomRedBall));
51         System.out.printf("中獎藍球號碼 %s\n",Arrays.toString(randomBlueBall));
52         
53         /**
54          * 判斷隨機球與輸入球號碼是否相同,相同就加 1
55          */
56         int sumRed = 0;
57         for(int i = 0; i<randomRedBall.length;i++) {
58             for(int j = 0; j<setRedBall.length;j++) {
59                 if(randomRedBall[i] == setRedBall[j]) {
60                     sumRed++;
61                 }
62             }
63         }
64         int sumBlue = 0;
65         if(randomBlueBall[0] == setBlueBall[0]) ++sumBlue;
66         
67         /**
68          * 匹配中獎機制
69          */
70         if(sumRed == 6 && sumBlue == 1) {
71             System.out.println("恭喜你獲得:一等獎");
72         }else if(sumRed == 6 && sumBlue == 0) {
73             System.out.println("恭喜你獲得:二等獎");
74         }else if(sumRed == 5 && sumBlue == 1) {
75             System.out.println("恭喜你獲得:三等獎");
76         }else if(sumRed == 5 && sumBlue == 0 || sumRed == 4 && sumBlue == 1) {
77             System.out.println("恭喜你獲得:四等獎");
78         }else if(sumRed == 4 && sumBlue == 0 || sumRed == 3 && sumBlue == 1) {
79             System.out.println("恭喜你獲得:五等獎");
80         }else if(sumRed == 2 && sumBlue == 1 || sumRed == 1 && sumBlue == 1 || sumRed == 0 && sumBlue == 1) {
81             System.out.println("恭喜你獲得:六等獎");
82         }else {
83             System.out.println("很遺憾,你沒中獎。多買幾次,總有機會的···");
84         }
85     }
86 
87 }

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM