生成新的随机数
本文关键字:随机数 | 更新日期: 2023-09-27 18:15:22
每次我点击按钮,应该产生一个新的随机数,数字只是保持不变。我怎么能有相同的变量,但新的数字onclick?
static Random random = new Random();
static int randomNumber1 = random.Next(0, 100);
static int randomNumber2 = random.Next(0, 100);
num1Label.Text = randomNumber1.ToString();
num2Label.Text = randomNumber2.ToString();
protected void submitAnswerButton_Click(object sender, EventArgs e)
{
answerStatus.Visible = true;
int counter = 0;
int answer = randomNumber1 + randomNumber2;
if (mathAnswerTextBox.Text == answer.ToString())
{
answerStatus.Text = "Correct!";
randomNumber1 = random.Next(2, 22);
randomNumber2 = random.Next(2, 222);
num1Label.Visible = true;
num2Label.Visible = true;
}
protected void submitAnswerButton_Click(object sender, EventArgs e)
{
answerStatus.Visible = true;
int counter = 0;
int answer = randomNumber1 + randomNumber2;
if (mathAnswerTextBox.Text == answer.ToString())
{
answerStatus.Text = "Correct!";
randomNumber1 = random.Next(2, 22);
randomNumber2 = random.Next(2, 222);
num1Label.Text = Convert.ToString(randomNumber1);//add this line
num2Label.Text = Convert.ToString(randomNumber2); //and this line
num1Label.Visible = true;
num2Label.Visible = true;
}
randomNumber1 = random.Next(2, 22);
randomNumber2 = random.Next(2, 222);
num1Label.Visible = true;
num2Label.Visible = true;
我看不出你在哪里把这两个新的随机数放入某种控制中。你使控件可见,但这并没有神奇地用新值填充它们。
如果不设置这些值,就会生成新的数字,但是用户永远不会看到它们。
您需要将num1Label
控件的文本属性更改为randomNumber1
的字符串值。第二个控件和数字也是如此。