生成新的随机数onclick

时间:2014-07-28 05:09:22

标签: c#

每次我点击应该生成新随机数的按钮时,数字都保持不变。我怎样才能拥有相同的变量,但是新的号码可以点击?

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;
    }

2 个答案:

答案 0 :(得分:0)

 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;
        }

答案 1 :(得分:0)

randomNumber1 = random.Next(2, 22);
randomNumber2 = random.Next(2, 222);
num1Label.Visible = true;
num2Label.Visible = true;

我看不到你实际上这两个新的随机数放入某种控件中。你正在使控件可见,但这并没有用新值神奇地填充它们。

如果不设置值,您将生成新数字,但用户却从未看到它们。

您需要将num1Label控件的textual属性更改为randomNumber1的字符串值。同上第二个控件和编号。