我想生成一个1到10之间的随机数。但是我收到了一些错误。C#生成随机数
public Form1()
{
InitializeComponent();
}
int randomNumber = (0, 11);
int attempts = 0;
public int RandomNumber
{
get
{
return randomNumber;
}
set
{
randomNumber = value;
}
}
它是所有在0,11逗号下是说 - >结构System.Int32表示32位有符号整数< - 。在11下它说 - >标识符预期的语法错误,','预计< - 。现在,如果我只是像int randomNumber = 0;那么它会正常工作,仍然有多次猜测,并且猜测数量会像它应该加起来一样,并且标签太高太低。只是数字将永远是0.
另外我怎样才能使它到我没有点击猜测按钮,我可以打在键盘上输入?
private void button1_Click_1(object sender, EventArgs e)
{
try
{
if (int.Parse(textBox1.Text) > RandomNumber) label1.Text = "Too high.";
else if (int.Parse(textBox1.Text) < RandomNumber) label1.Text = "Too low.";
else
{
label1.Text = "You won.";
textBox1.Enabled = false;
label2.Text = "Attempts: 0";
textBox1.Text = String.Empty;
MessageBox.Show("You won in " + attempts + " attempts, press generate to play again.", "Winner!");
attempts = 0;
label2.Text = "Attempts: " + attempts.ToString();
return;
}
attempts++;
label2.Text = "Attempts: " + attempts.ToString();
}
catch { MessageBox.Show("Please enter a number."); }
}
C#没有用于生成随机数的特殊语法。如果你想产生一个随机数,看看'随机'类。 – Luaan
http://stackoverflow.com/questions/2706500/how-do-i-generate-a-random-int-number-in-c – daremachine
你可以在这里找到答案:http://stackoverflow.com/questions/ 2706500 /我怎么生成一个随机-int-number-in-c – Breeze