Try   HackMD

期末考 / 骰子遊戲:比大小

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

外層初值

public partial class Form1 : Form { int success = 0; int error = 0;

表單

public Form1() { InitializeComponent(); label8.Text = "5"; label9.Text = "0"; label10.Text = "0"; //按鈕關閉 button1.Enabled = false; button2.Enabled = false; button4.Enabled = false; }

偶數

private void button1_Click(object sender, EventArgs e) { int[] randomArray = new int[5]; int aws; Random rnd = new Random(); for (int i = 0; i < 5; i++) { randomArray[i] = rnd.Next(1, 10); for (int j = 0; j < i; j++) { while (randomArray[j] == randomArray[i]) { j = 0; randomArray[i] = rnd.Next(1, 10); } } } label8.Text = (Convert.ToInt32(label8.Text) - 1).ToString(); label6.Text = randomArray[0].ToString(); label7.Text = randomArray[1].ToString(); aws = randomArray[0] + randomArray[1]; if (aws % 2 == 0) { MessageBox.Show("恭喜你!!!猜中了", "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); success += 1; label9.Text = success.ToString(); } else { MessageBox.Show("ㄛ喔!!!猜錯了", "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); error += 1; label10.Text = error.ToString(); } if (Convert.ToInt32(label8.Text) == 0) { MessageBox.Show("遊戲結束 ,按重設以開始遊戲"); button1.Enabled = false; button2.Enabled = false; button3.Enabled = false; button4.Enabled = true; } }

奇數

private void button2_Click(object sender, EventArgs e) { int[] randomArray = new int[5]; int aws; Random rnd = new Random(); for (int i = 0; i < 5; i++) { randomArray[i] = rnd.Next(1, 10); for (int j = 0; j < i; j++) { while (randomArray[j] == randomArray[i]) { j = 0; randomArray[i] = rnd.Next(1, 10); } } } label8.Text = (Convert.ToInt32(label8.Text) - 1).ToString(); label6.Text = randomArray[0].ToString(); label7.Text = randomArray[1].ToString(); aws = randomArray[0] + randomArray[1]; if (aws % 2 == 0) { MessageBox.Show("ㄛ喔!!!猜錯了", "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); error += 1; label10.Text = error.ToString(); } else { MessageBox.Show("恭喜你!!!猜中了", "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); success += 1; label9.Text = success.ToString(); } if (Convert.ToInt32(label8.Text) == 0) { MessageBox.Show("遊戲結束 ,按重設以開始遊戲"); button1.Enabled = false; button2.Enabled = false; button3.Enabled = false; button4.Enabled = true; } }

重設

private void button4_Click(object sender, EventArgs e) { success = 0; error = 0; label6.Text = ""; label7.Text = ""; label8.Text = "5"; label9.Text = "0"; label10.Text = "0"; button1.Enabled = false; button2.Enabled = false; button3.Enabled = true; button4.Enabled = false; }