在c#中保留一个随机数
本文关键字:一个 随机数 保留 | 更新日期: 2023-09-27 18:13:21
我的程序设置为让用户猜测一个介于1和10之间的整数。如果用户猜测过低或过高,则会通知他们并可以重试。
我遇到的问题是,当用户猜测错误时,会生成一个新的随机数。因此,从本质上讲,用户在弄错后永远不会试图猜测相同的数字。
我需要这样做,当用户猜测错误时,他们仍然试图猜测相同的值。
这是我的代码:
namespace IntegerGame
{
public partial class guessGame : Form
{
int num1;
int num2;
public guessGame()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void guessButton_Click(object sender, EventArgs e)
{
Random rnd1 = new Random();
num1 = rnd1.Next(1, 10);
if (int.TryParse(textBox1.Text, out num2))
{
if (num2 < 0 || num2 > 10)
{
textBox1.Clear();
MessageBox.Show("Please enter a number between 1 and 10");
}
else
{
if (num2 > num1)
{
textBox1.Clear();
MessageBox.Show("You guessed to high, please try again");
}
else if (num2 < num1)
{
textBox1.Clear();
MessageBox.Show("You guessed to low, please try again");
}
else if (num2 == num1)
{
textBox1.Clear();
MessageBox.Show("You guessed " + num2 + ", which was the right number!!");
}
}
}
else
{
textBox1.Clear();
MessageBox.Show("This is not a valid integer, please enter a valid integer");
}
}
}
}
生成随机数作为guessGame的成员(或在构造函数中,在InitializeComponent之后(,而不是每当用户按下按钮时
public partial class guessGame : Form
{
Random rnd1 = new Random();
int num1 = rnd1.Next(1, 10);
int num2;
...
每次单击按钮时,都会运行以下代码:
Random rnd1 = new Random();
num1 = rnd1.Next(1, 10);
这意味着每次用户猜测时,都会生成一个新的随机数。
我建议制作随机和随机数字段(编辑:注意到你的数字已经是一个字段(,对于初始字段,在构造函数中创建它,如下所示:
private Random _rnd1;
private int num1;
GuessGame()
{
_rnd1 = new Random();
_num1 = _rnd1.Next(1,10);
InitializeComponent();
}
然后,当用户猜测正确时,您可以简单地通过将_num1
字段设置为随机数中的下一个数字来生成一个新数字。
在生成随机数时检查是否需要生成数字
Random rnd1 = new Random();
int num1 = -1;
private void guessButton_Click(object sender, EventArgs e)
{
if (num1 == -1)
{
num1 = rnd1.Next(1, 10);
}
//...
//assign -1 to num1 after successful guess.
num1 = -1;
}
初始化表单时,应该设置一个随机值。只有当表单关闭并重新打开时,它才会更改。
Random rnd1 = new Random();
public guessGame()
{
InitializeComponent();
num1 = rnd1.Next(1, 10);
}
只需在构造函数中生成随机数,而不是在点击处理程序中生成:
namespace IntegerGame
{
public partial class guessGame : Form
{
int num1;
int num2;
public guessGame()
{
Random rnd1 = new Random();
num1 = rnd1.Next(1, 10);
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void guessButton_Click(object sender, EventArgs e)
{
if (int.TryParse(textBox1.Text, out num2))
{
if (num2 < 0 || num2 > 10)
{
textBox1.Clear();
MessageBox.Show("Please enter a number between 1 and 10");
}
else
{
if (num2 > num1)
{
textBox1.Clear();
MessageBox.Show("You guessed to high, please try again");
}
else if (num2 < num1)
{
textBox1.Clear();
MessageBox.Show("You guessed to low, please try again");
}
else if (num2 == num1)
{
textBox1.Clear();
MessageBox.Show("You guessed " + num2 + ", which was the right number!!");
}
}
}
else
{
textBox1.Clear();
MessageBox.Show("This is not a valid integer, please enter a valid integer");
}
}
}}
namespace IntegerGame
{
public partial class guessGame : Form
{
int num1;
int num2;
Random rnd1 = new Random();
public guessGame()
{
InitializeComponent();
num1 = rnd1.Next(1, 10);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void guessButton_Click(object sender, EventArgs e)
{
if (int.TryParse(textBox1.Text, out num2))
{
if (num2 < 0 || num2 > 10)
{
textBox1.Clear();
MessageBox.Show("Please enter a number between 1 and 10");
}
else
{
if (num2 > num1)
{
textBox1.Clear();
MessageBox.Show("You guessed to high, please try again");
}
else if (num2 < num1)
{
textBox1.Clear();
MessageBox.Show("You guessed to low, please try again");
}
// Note that this could be an else: it is the only case left
else if (num2 == num1)
{
num1 = rnd1.Next(1, 10);
textBox1.Clear();
MessageBox.Show("You guessed " + num2 + ", which was the right number!!");
}
}
}
else
{
textBox1.Clear();
MessageBox.Show("This is not a valid integer, please enter a valid integer");
}
您只想在它们相等时生成数字,而不是在按下按钮时生成数字。