看板 C_Sharp 關於我們 聯絡資訊
我的做法是將答案亂數藏在 UI 的 Tag 屬性中 在由 Button 的 click callback 去檢查答案 Code如下 給你參考看看 希望對你有幫助 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= public partial class Form1 : Form { int times = 0; // 猜錯計數器 Random random = new Random(); public Form1() { InitializeComponent(); SetTag(); // 遊戲ㄧ開始 初始化四個button的tag值 藏答案 } // Form1() // 把四個按鈕的click事件 加入此callback 檢查答案 private void button1_Click(object sender, EventArgs e) { if (((Button)sender).Tag.Equals(0)) // tag值是0 代表猜對 { label1.Text = "猜對"; SetTag(); // 初始化四個button的tag值 藏答案 } // if else { times++; // 猜錯計數器加1 label1.Text = "猜錯"+times.ToString()+"次"; ((Button)sender).Enabled = false; if (times == 3) { label1.Text = "三次了"; SetTag(); // 初始化四個button的tag值 藏答案 } // if } // else } // click callback public void SetTag() // 初始化四個button的tag值 { int value = random.Next(); button1.Tag = value % 4; button2.Tag = (value+1) % 4; button3.Tag = (value+2) % 4; button4.Tag = (value+3) % 4; button1.Enabled = true; button2.Enabled = true; button3.Enabled = true; button4.Enabled = true; times = 0; // 猜錯計數器歸0 } // SetTag() } // class Form1 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 111.184.53.202 ※ 編輯: voltslin 來自: 111.184.53.202 (04/17 02:53)
tongzhou:個人覺得這篇可以m @@ 04/22 20:44