因此,我正在为一个在1-100之间随机生成的数字在猜谜游戏中的学校项目工作。目前我唯一遇到的问题是,每当用户输入一个新号码时,生成的号码也会改变。我试着将代码放在表单加载器中生成数字,但后来在程序中无法访问它。这是迄今为止我所拥有的。在c中的随机数猜谜游戏的麻烦#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void guessButton_Click(object sender, EventArgs e)
{
int userGuess;
userGuess = int.Parse(guessText.Text);
Random rand = new Random();
int number = rand.Next(1, 100);
label2.Text = "" + number;
if (userGuess > number)
{
resultLabel.Text = "Your guess is too high";
}
else if (userGuess < number)
{
resultLabel.Text = "Your guess is too low.";
}
else if (userGuess == number)
{
resultLabel.Text = "That is correct!";
}
guessText.Clear();
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
http://csharpindepth.com/Articles/Chapter12/Random.aspx – Oded 2014-10-11 19:11:15
形式装载机在正确的位置把它。查找哪些字段和成员变量。 – 2014-10-11 19:13:15
@MobyDisk如果表单被关闭并打开,那么随机数仍然会每次都会重新生成。 – cybermonkey 2014-10-11 19:16:19