我正在嘗試爲我製作的簡單數字猜測遊戲添加代碼。一旦用戶猜到了正確的號碼,就會彈出消息對話框,告訴他他是勝利者。當我點擊它時,該消息對話框中有一個名爲「ok」的按鈕,我將回到表單。當用戶點擊消息對話框「ok」按鈕時重新啓動代碼
相反,我希望它重新啓動代碼,以便生成一個新的隨機數。這是可能的還是我需要手動將代碼恢復到優勝者代碼區域內的默認狀態?
這裏是我現在的代碼:
private void btnEval_Click (object sender, EventArgs e)
{
// increment the counter to be displayed later
howManyClicks++;
// main decision conditions, ensures something is entered
if (txtNum.Text != "")
{
// user input number
double num = double.Parse (txtNum.Text);
if (randomNumber > num)
{
// if too low
this.BackColor = Color.Yellow;
lblMain.Text = "TOO LOW!";
lblReq.Text = "please try again";
txtNum.Clear();
txtNum.Focus();
}
else if (randomNumber < num)
{
// if too high
this.BackColor = Color.Red;
lblMain.Text = "TOO HIGH!";
lblReq.Text = "please try again";
txtNum.Clear();
txtNum.Focus();
}
else
{
// correct
this.BackColor = Color.Green;
lblMain.Text = "CORRECT!";
lblReq.Text = "well done";
MessageBox.Show ("You are right!! It took you " + howManyClicks + " guesses", "You are a WINNER!!",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
txtNum.Clear();
txtNum.Focus();
}
}
else
{
MessageBox.Show ("You must enter a vaild number! Please try again.", "ERROR",
MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
txtNum.Clear();
txtNum.Focus();
}