有没有大神会c#实现如下功能:猜数字游戏
要求:用图形用户界面实现。
功能:计算机产生随机数,猜中即胜,猜不中,提示是大了还是小了,继续猜,直至猜到,给出所用时间和评语。 展开
我来试试(滑稽),也是自学没多久
来了,初学者,大神勿喷,用的都是基础的方法:
private void btnStart_Click(object sender, EventArgs e)
{
btnStart.Visible = false;
panel1.Visible = true;
int i = GetRandom(10);
randomNum.Text = "生成的随机数为:" + i;
randomNum.Tag = i;
btnReset.Visible = false;
randomNum.Visible = false;
labTip.Visible = false;
textBox1.Text = "";
button1.Visible = true;
}
private int GetRandom(int MaxValue)
{
Random rand = new Random();
int rd = rand.Next(1, MaxValue);
return rd;
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
labTip.Text = "请输入您猜测的数字";
labTip.Visible = true;
}
else
{
if (Convert.ToInt32(textBox1.Text) < Convert.ToInt32(randomNum.Tag))
{
labTip.Text = "您猜测的数字小了,再试试!";
labTip.Visible = true;
}
else if (Convert.ToInt32(textBox1.Text) > Convert.ToInt32(randomNum.Tag))
{
labTip.Text = "您猜测的数字大了,再试试!";
labTip.Visible = true;
}
else if (Convert.ToInt32(textBox1.Text) == Convert.ToInt32(randomNum.Tag))
{
labTip.Text = "恭喜您,猜对了!";
labTip.Visible = true;
randomNum.Visible = true;
btnReset.Visible = true;
button1.Visible = false;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
btnStart.Visible = true;
panel1.Visible = false;
}
2022-03-17 广告