我正在尝试为我正在制作的一个简单的数字猜谜游戏添加我的代码。一旦用户猜到正确的数字,就会弹出一个消息对话框,告诉他他是赢家。该消息对话框有一个名为“确定”的按钮,当我单击它时,我将返回表单。
相反,我希望它重新启动代码,以便生成一个新的随机数。这是可能的还是我需要手动将代码恢复到获胜者代码区域内的默认状态?
这是我现在的代码:
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 ();
}