這次程式是改版之前第一板之後的第二板,這次多學會一些Linq的技巧,放在這個小專案來練習一下,順便想起要中頭獎的機率低到還是回頭寫Code來得實際。
buy:購買一張彩券,看是否有中獎。
clear: 清除之前購買的彩券紀錄,重新統計資料。
autobuy:自動購買彩券直到中獎為止,若機率太低程式會跑一段時間。
程式碼如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Windows.Forms; namespace Mylotterytest { public partial class Form1 : Form { private Random myWinnum = new Random(); private Label[] lbwinnum = new Label[6]; private TextBox[] tbnum = new TextBox[6]; private double count = 0; private double wincount = 0; private int maxNum = 36; private int setMatch = 3; public Form1() { InitializeComponent(); lbwinnum[0] = lb_win1; lbwinnum[1] = lb_win2; lbwinnum[2] = lb_win3; lbwinnum[3] = lb_win4; lbwinnum[4] = lb_win5; lbwinnum[5] = lb_win6; tbnum[0] = tb_lot1; tbnum[1] = tb_lot2; tbnum[2] = tb_lot3; tbnum[3] = tb_lot4; tbnum[4] = tb_lot5; tbnum[5] = tb_lot6; } private void btn_buy_Click(object sender, EventArgs e) { List<int> numlist = new List<int>() ; List<int> buynumlist = new List<int>(); maxNum = Convert.ToInt32(tb_maxNum.Text) + 1; for (int i = 0; i < 6; i++) { buynumlist.Add(Convert.ToInt32(tbnum[i].Text)); } RandomList(numlist); for (int i = 0; i < lbwinnum.Length; i++) { lbwinnum[i].Text = numlist[i].ToString(); } var SameNum = numlist.Where(i => buynumlist.Contains(i)).Count(); lb_match.Text = SameNum.ToString(); lb_buycount.Text = count.ToString(); count++; if (SameNum >= 3) MessageBox.Show("3 Match!"); } private void btn_countclear_Click(object sender, EventArgs e) { count = 0; wincount = 0; } private void RandomList(List<int> numlist) { for (int i = 0; i < 6; i++) numlist.Add(myWinnum.Next(1, maxNum)); var same = numlist.GroupBy(x => x).Any(g => g.Count() > 1); while (same) { numlist.Clear(); for (int i = 0; i < 6; i++) numlist.Add(myWinnum.Next(1, maxNum)); same = numlist.GroupBy(x => x).Any(g => g.Count() > 1); } numlist.Sort(); } private void btn_autobuy_Click(object sender, EventArgs e) { List<int> numlist = new List<int>(); List<int> buynumlist = new List<int>(); maxNum = Convert.ToInt32(tb_maxNum.Text) + 1; setMatch = Convert.ToInt32(tb_setMatch.Text); //count = 0; RandomList(numlist); RandomList(buynumlist); var SameNum = numlist.Where(i => buynumlist.Contains(i)).Count(); count++; while (SameNum < setMatch) { numlist.Clear(); buynumlist.Clear(); RandomList(numlist); RandomList(buynumlist); SameNum = numlist.Where(i => buynumlist.Contains(i)).Count(); count++; } wincount++; for (int i = 0; i < lbwinnum.Length; i++) { lbwinnum[i].Text = numlist[i].ToString(); tbnum[i].Text = buynumlist[i].ToString(); } lb_match.Text = SameNum.ToString(); lb_matchcount.Text = wincount.ToString(); lb_buycount.Text = count.ToString(); lb_winrate.Text = ((wincount / count) * 100).ToString("0.00000000") + "%"; } } } |
操作起來非常簡單,只需要設定球池最大數以及配對成功數量即可運轉。
裡面有使用Linq中的GroupBy算是很初階的使用方式,學了Linq才發現程式好玩的地方,如傳言學習Linq就回不去了。好用簡潔方便維護的程式碼真的好吃又不黏牙。