【发布时间】:2016-08-20 17:37:22
【问题描述】:
我想生成一个介于 1 和 10 之间的随机数。但我遇到了几个错误。
public Form1()
{
InitializeComponent();
}
int randomNumber = (0, 11);
int attempts = 0;
public int RandomNumber
{
get
{
return randomNumber;
}
set
{
randomNumber = value;
}
}
全部放在0、11下面的逗号就是说--> struct System.Int32代表一个32位有符号整数 标识符预期语法错误,',' 预期
另外,我怎样才能到达我不必点击猜测按钮的地方,我只需按键盘上的 Enter 键即可?
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# 没有用于生成随机数的特殊语法。如果要生成随机数,请查看
Random类。 -
你可以在这里找到答案:stackoverflow.com/questions/2706500/…
-
"另外,我怎样才能到达我不必点击猜测按钮的地方,我可以直接在键盘上按 Enter 键?"这是一个与随机数无关的单独问题。将其作为一个新问题提出,您会得到更好的答案。