Step 2: Create a Random Addition Problem

The quiz needs math problems. If the quiz repeats the same problems, it's not challenging, so you want to include random numbers. You add a method called StartTheQuiz() that fills in the problems and starts the countdown timer. For this step, you add a random addition problem. The other math problems and countdown timer are added in later steps in this tutorial.

In tutorial 2, you created several SoundPlayers for a maze game. You do the same for the math quiz, except instead of the SoundPlayer class, you use the Random class.

To create a random addition problem

  1. Create a Random object by using a new statement like the following.

    Public Class Form1
    
        ' Create a Random object to generate random numbers.
        Private randomizer As New Random
    
    public partial class Form1 : Form
    {
        // Create a Random object to generate random numbers.
        Random randomizer = new Random();
    

    You have now added a Random object to your form and called it randomizer.

    Note

    In the maze tutorial, you created two SoundPlayer components using the new statement. This does something similar. The only difference is that, unlike SoundPlayer, Random isn't a component, and it isn't a control, so it can't be called by those names. It's called an object. You've probably heard the word object before, and you learn more about what it means in the next few tutorials. For now, all you need to know is that when your program uses a new statement to create buttons, labels, panels, OpenFileDialogs, ColorDialogs, SoundPlayers, Randoms, and even forms, the item that gets created is called an object. Later tutorials show you much more about how these objects work.

  2. Now when you start your form, it creates a new Random object and gives it the name randomizer. Like with SoundPlayers, if you go into a method and start to type randomizer, and then dot (.), an IntelliSense window opens showing you all of the Random object's methods that you can call. You use the Next() method, as follows.

    Next method

    Next method

    When you call random.Next(50), you get a random number that's less than 50 (from 0 through 49).

  3. Soon you will build a method to check the answers, so your program needs to remember what numbers it chose for the problems. Add an integer (known as an int in C# or Integer in Visual Basic) called addend1 and an int (Integer) called addend2 to the form (just like you added a Random object called randomizer), as follows.

    ' Create a Random object to generate random numbers.
    Private randomizer As New Random
    
    ' These Integers will store the numbers
    ' for the addition problem.
    Private addend1 As Integer
    Private addend2 As Integer
    
    // Create a Random object to generate random numbers.
    Random randomizer = new Random();
    
    // These ints will store the numbers
    // for the addition problem.
    int addend1;
    int addend2;
    

    Note

    An int (Integer) is used to store a positive or negative number value. It can hold any number from -2147483648 through 2147483647. It can only store whole numbers, and not decimals.

  4. Next, add a method called StartTheQuiz() that uses the Random object Next() method to choose two numbers and place the numbers in the labels. It will eventually fill in all of the problems and then start the timer, so add a comment. It should look like the following.

    ''' <summary>
    ''' Start the quiz by filling in all of the problems
    ''' and starting the timer.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub StartTheQuiz()
        ' Fill in the addition problem.
        addend1 = randomizer.Next(51)
        addend2 = randomizer.Next(51)
    
        plusLeftLabel.Text = addend1.ToString()
        plusRightLabel.Text = addend2.ToString()
    
        sum.Value = 0
    End Sub
    
    /// <summary>
    /// Start the quiz by filling in all of the problems
    /// and starting the timer.
    /// </summary>
    public void StartTheQuiz()
    {
        // Fill in the addition problem.
        addend1 = randomizer.Next(51);
        addend2 = randomizer.Next(51);
    
        plusLeftLabel.Text = addend1.ToString();
        plusRightLabel.Text = addend2.ToString();
    
        sum.Value = 0;
    }
    

    Note

    Note that you called randomizer.Next(51). The reason 51 and not 50 is used is so the two numbers add up to an answer that's from 0 through 100. If you pass 50 to the Next() method, it chooses a number from 0 through 49, so the highest possible answer is 98, and not 100. After the first two statements in the method execute, each of the two ints (Integers), addend1 and addend2, hold a random number from 0 through 50.

    Take a closer look at these statements.

    plusLeftLabel.Text = addend1.ToString()
    plusRightLabel.Text = addend2.ToString()
    
    plusLeftLabel.Text = addend1.ToString();
    plusRightLabel.Text = addend2.ToString();
    

    The statements set the Text properties of the two plus labels, plusLeftLabel and plusRightLabel, so that the labels display the two random numbers. You need to use the int's (Integer's) ToString() method to convert it to text (in programming, string means text), because Label controls only display text, and not numbers.

  5. You want the Start button to start the quiz, so go to Windows Forms Designer and double-click the button to add a Click event handler. Then add the following two statements.

    Private Sub startButton_Click() Handles startButton.Click
        StartTheQuiz()
        startButton.Enabled = False
    End Sub
    
    private void startButton_Click(object sender, EventArgs e)
    {
        StartTheQuiz();
        startButton.Enabled = false;           
    }
    

    You know what the first statement does: It calls the new StartTheQuiz() method. The second statement sets the startButton control Enabled property to False. That disables the button, so the user can't click it. That way, the user can only click the Start button once. After that, the button appears dimmed and is unavailable, and the user must finish the quiz before the time runs out (or close the program).

  6. Now save and run your program. Click the Start button. A random addition problem should appear, as shown in the following picture.

    Random addition problem

    Random addition problem

To continue or review