抓彩票。
Math.random()用起來比Random包簡單,不過功能也差一點。
此外就是簡單的JOptionPane調用以及各種條件循環語句。
沒啥好講的,簡單的跟狗一樣。

1 package Lottery;
2
3 /**Name: Lottery
4 * Author: mlxy
5 * Date: 2013-11-18
6 * Version: 1.0
7 * Memo: 内容一運作就懂,沒什麼特别的營養。
8 */
9
10 import javax.swing.JOptionPane;
11
12 public class Run {
13
14 public static void main(String[] args) {
15
16 //初始化錢數。
17 int money = 5000;
18
19 JOptionPane.showMessageDialog(null, "新一期彩票投注開始啦!", "試手氣,中鹵,不,大獎!", JOptionPane.WARNING_MESSAGE);
20
21 while (true) {
22
23 //生成彩票号碼。
24 int lottery = (int) (Math.random() * 100);
25
26 while (true) {
27
28 //使用者輸入彩票号碼。
29 String input = JOptionPane.showInputDialog(null,
30 "請輸入你想要的彩票号碼(兩位數):", "一注500元", JOptionPane.PLAIN_MESSAGE);
31
32 //檢查并擷取輸入内容。
33 if (input.length() == 0 || !input.matches("\\d{2}")) {
34 JOptionPane.showMessageDialog(null, "輸入錯誤,請重新輸入");
35 continue;
36 }
37 int userChoose = Integer.parseInt(input);
38
39 //扣錢。
40 money -= 500;
41
42 //先行判斷使用者是否猜中。
43 if (userChoose == lottery) {
44 JOptionPane.showMessageDialog(null, "完全命中,獎金10000元!", "特等獎", JOptionPane.WARNING_MESSAGE);
45 money += 10000;
46 break;
47 }
48
49 int lottery1 = lottery / 10; //十位數字。
50 int lottery2 = lottery % 10; //個位數字。
51
52 int user1 = userChoose / 10;
53 int user2 = userChoose % 10;
54
55 if (lottery1 == user2 && lottery2 == user1) {
56 JOptionPane.showMessageDialog(null, "錯位命中,獎金3000元!", "一等獎", JOptionPane.WARNING_MESSAGE);
57 money += 3000;
58 break;
59 } else if (lottery1 == user1 || lottery1 == user2 || lottery2 == user1 || lottery2 == user2) {
60 JOptionPane.showMessageDialog(null, "命中一個數字,獎金1000元!", "二等獎", JOptionPane.WARNING_MESSAGE);
61 money += 1000;
62 break;
63 } else {
64 JOptionPane.showMessageDialog(null, "沒命中,真遺憾。", "下次再來", JOptionPane.PLAIN_MESSAGE);
65 break;
66 }
67 }
68
69 JOptionPane.showMessageDialog(null, "中獎号碼是:" + lottery + "\n你現在還剩" + money + "元。", "号碼揭曉", JOptionPane.PLAIN_MESSAGE);
70
71 //健♂壯的程式。
72 if (money > 2100000000) {
73 JOptionPane.showMessageDialog(null, "大爺您怎麼搞這麼多錢來的小廟容不下您這菩薩您快走吧别玩了。", "挂B死全家", JOptionPane.WARNING_MESSAGE);
74 break;
75 } else if (money < 100) {
76 JOptionPane.showMessageDialog(null, "你沒錢啦,以後再來吧。", "窮鬼一邊去", JOptionPane.WARNING_MESSAGE);
77 break;
78 }
79 }
80 }
81 }
謝謝惠顧