We recommend using Visual Studio 2017

Step 6: Add a Subtraction Problem

To add a subtraction problem, you need to:

• Store the subtraction values.

• Generate random numbers for the problem (and be sure that the answer is between 0 and 100).

• Update the method that checks the answers so that the method checks the new subtraction problem too.

• Update your timer's Tick event handler so that the event handler fills in the correct answer when time runs out.

1. First, you need a place to store the values, so add two ints (Integers) for the subtraction problem to your form. The new code appears between the addition integers and the timer integer. The code should look like the following.

public partial class Form1 : Form
{
// Create a Random object to generate random numbers.
Random randomizer = new Random();

// These ints will store the numbers

// These ints will store the numbers
// for the subtraction problem.
int minuend;
int subtrahend;

// This int will keep track of the time left.
int timeLeft;

Note

The names of the new ints—minuend and subtrahend—aren't programming terms. They are the traditional names in arithmetic for the number that's being subtracted (the subtrahend) and the number being subtracted from (the minuend). The difference is the minuend minus the subtrahend. You could use other names, because your program doesn't require specific names for ints, controls, components, or methods. There are some rules (for example, names can't begin with digits), but in general, you could use names such as x1, x2, x3, x4, and so on. But it would be difficult to read the code, and nearly impossible to track problems. You will use the traditional names for multiplication (multiplicand × multiplier = product) and division (dividend ÷ divisor = quotient) later in this tutorial.

2. Next, modify the StartTheQuiz() method to fill in a random subtraction problem. The new code follows the "Fill in the subtraction problem" comment. The code should look like the following.

/// <summary>
/// Start the quiz by filling in all of the problems
/// and starting the timer.
/// </summary>
public void StartTheQuiz()
{
// Fill in the addition problem.
sum.Value = 0;

// Fill in the subtraction problem.
minuend = randomizer.Next(1, 101);
subtrahend = randomizer.Next(1, minuend);
minusLeftLabel.Text = minuend.ToString();
minusRightLabel.Text = subtrahend.ToString();
difference.Value = 0;

// Start the timer.
timeLeft = 30;
timeLabel.Text = "30 seconds";
timer1.Start();
}

This code uses the Random class Next() method a little differently. When you give it two values, it picks a random number that's greater than or equal to the first one and less than the second one. The following line chooses a random number from 1 through 100, and stores it in minuend.

minuend = randomizer.Next(1, 101);

The Random class Next() method can be called in several ways. When you can call a method in more than one way, that's called an overloaded method, and you can use IntelliSense to explore this. Take another look at the IntelliSense window tooltip for the Next() method.

Intellisense window tooltip

Notice how the tooltip shows (+ 2 overload(s)). This means that there are two other ways that you can call the Next() method. When you type the new code for the StartTheQuiz() method, you can see more information. As soon as you type randomizer.Next(, the IntelliSense window opens. Press the UP ARROW and DOWN ARROW keys to cycle through the overloads, as shown in the following picture.

The one in the preceding picture is the one you want, because it lets you specify a minimum and maximum value.

3. Modify the CheckTheAnswer() method to check for the correct subtraction answer. The code should look like the following.

/// <summary>
/// Check the answer to see if the user got everything right.
/// </summary>
/// <returns>True if the answer's correct, false otherwise.</returns>
{
&& (minuend - subtrahend == difference.Value))
return true;
else
return false;
}

The && is the Visual C# logical and operator. In Visual Basic, the equivalent operator is AndAlso. It's the same as saying, "If addend1 plus addend2 is equal to the value of the sum NumericUpDown, and if minuend minus subtrahend is equal to the value of the difference NumericUpDown." The CheckTheAnswer() method only returns true if the addition problem is correct and the subtraction problem is correct.

4. Change the last part of the timer's Tick event handler so it fills in the correct answer when time runs out. The code should look like the following.

else
{
// If the user ran out of time, stop the timer, show
// a MessageBox, and fill in the answers.
timer1.Stop();
timeLabel.Text = "Time's up!";
MessageBox.Show("You didn't finish in time.", "Sorry");