mercredi 18 août 2021

How do I use a random number I generated in a separate sum

I'm a complete beginner and trying to create a Times Table game app that asks the user to input a number, generates a multiplication sum, and then checks if it's the write answer to deliver a congratulations message and then adds the score.

I've managed to generate the random number, but when I input the correct answer I get the Incorrect message which makes me think that the random number is not pulling through to the second button. Any help much appreciated!

Button 1

private void button1_Click(object sender, EventArgs e)
{
    userInput = Convert.ToInt32(textBox1.Text);
    if (userInput >= 1 && userInput <= 10)
    {
        Random rNum = new Random();   //uses built in function Random()
        int rrNum = rNum.Next(1, 12);  // creates a number between 1 and 12
        label5.Text = (Convert.ToString(rrNum) + " x " + userInput);
    }
}

Button 2

private void button2_Click(object sender, EventArgs e)
{
    userAnswer = Convert.ToInt32(textBox2.Text);
    Random rNum = new Random();
    int rrNum = rNum.Next(1, 12);
    if (userAnswer == userInput * rrNum)
    {
        label8.Text = "Congratulations!";
        pictureBox1.Enabled = true;
        pictureBox1.Visible = true;
    }
    else
    {
        label8.Text = ("Incorrect, try again!");
    }
}

    



Aucun commentaire:

Enregistrer un commentaire