Anchorage Finance

Mar 13 2018

Step 6: Add a Subtraction Problem, answer to an addition problem.#Answer #to #an #addition #problem


Step 6: Add a Subtraction Problem

Answer to an addition problem

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on

The latest version of this topic can be found at Step 6: Add a Subtraction Problem.

In the sixth part of this tutorial, you’ll add a subtraction problem and learn how to perform the following tasks:

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 it 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.

To add a subtraction problem

Add two integer variables for the subtraction problem to your form, between the integer variables for the addition problem and the timer. The code should look like the following.

The names of the new integer variables—minuend and subtrahend—aren’t programming terms. They’re the traditional names in arithmetic for the number that’s being subtracted (the subtrahend) and the number from which the subtrahend is being subtracted (the minuend). The difference is the minuend minus the subtrahend. You could use other names, because your program doesn’t require specific names for variables, controls, components, or methods. You must follow rules such as not starting names with digits, but you can generally use names such as x1, x2, x3, and x4. However, generic names make code difficult to read and problems nearly impossible to track down. To keep variable names unique and helpful, you’ll use the traditional names for multiplication (multiplicand × multiplier = product) and division (dividend ÷ divisor = quotient) later in this tutorial.

Next, you’ll modify the StartTheQuiz() method to provide random values for the subtraction problem.

Add the following code after the “Fill in the subtraction problem” comment.

To prevent negative answers for the subtraction problem, this code uses the Next() method of the Random class a little differently from how the addition problem does. When you give the Next() method two values, it picks a random number that’s greater than or equal to the first value and less than the second one. The following code chooses a random number from 1 through 100 and stores it in the minuend variable.

You can call the Next() method of the Random class, which you named “randomizer” earlier in this tutorial, in multiple ways. Methods that you can call in more than one way are referred to as overloaded, and you can use IntelliSense to explore them. Look again at the tooltip of the IntelliSense window for the Next() method.

Answer to an addition problem

Intellisense window tooltip

The tooltip shows (+ 2 overload(s)), which means that you can call the Next() method in two other ways. Overloads contain different numbers or types of arguments, so that they work slightly differently from one another. For example, a method might take a single integer argument, whereas one of its overloads might take an integer and a string. You choose the correct overload based on what you want it to do. When you add the code to the StartTheQuiz() method, more information appears in the Intellisense window as soon as you enter randomizer.Next( . Choose the Up Arrow and Down Arrow keys to cycle through the overloads, as the following illustration shows.

Answer to an addition problem

Overload for Next() method in IntelliSense

In this case, you want to choose the last overload, because you can specify minimum and maximum values.

Modify the CheckTheAnswer() method to check for the correct subtraction answer.

Written by admin

Leave a Reply

Your email address will not be published. Required fields are marked *