如果您检查有多少次尝试让用户猜出随机数,那么您需要修复随机数,并在启动游戏时全局尝试或在方块外尝试。
示例:
int triesInteger = 0;
int randomnum = new Random().Next(0, 10);
private void button3_Click(object sender, EventArgs e)
{
int guessInteger;
if (int.TryParse(textBoxcommanf.Text, out guessInteger))
{
if (guessInteger < randomnum)
{
triesInteger++;
answerLabel.Text = "You are too low";
numTriesLabel.Text = "" + triesInteger;
}
if (guessInteger > randomnum)
{
triesInteger++;
answerLabel.Text = "You are too high";
numTriesLabel.Text = "" + triesInteger;
}
if (guessInteger == randomnum)
{
triesInteger++;
answerLabel.Text = "Correct, Way to go!";
numTriesLabel.Text = "" + triesInteger;
}
}
else
{
answerLabel.Text = "Number is invalid";
}
}
它会创建一个随机数,然后用户开始猜测该数字,如果猜错显示提示(高或低),每个用户都会尝试增加计数。
如果要重新开始,则需要在某些按钮上单击重置值。然后重新开始猜数字游戏。
private void button4_Click(object sender, EventArgs e)
{
triesInteger = 0;
randomnum = new Random().Next(0, 10);
answerLabel.Text = "";
numTriesLabel.Text = "0";
}