Lesson 17: Making a Multiplication Program

In the last lesson, we learned about the Scanner class and how we can use it to get a user’s input.  In this lesson, we are going to be working again with the Scanner class in order to create a simple multiplication program for integers!  

First, let’s start off by importing the Scanner class, creating a class called IntegerCalculator, and creating a Scanner object and naming it scan .  It should look like the code below.

Remember that the Java comments do not affect the outcome of the code, it is just there to help others read/understand your code.

Now that we have our Scanner, we will now print out a set of instructions asking them to input an number and press Enter.  For the next line, we will create an int called userInput1 and make it equal to the scan.nextInt() .

Remember that scan is the name we gave to our Scanner and nextInt() is a method from the Scanner class we imported.

Now, we repeat the steps taken above by printing out another set of instructions asking the user to input their 2nd number and creating another int variable named userInput2 that is assigned to scan.nextInt().

Excellent, we have the input we need!  Both of the numbers that the user wants multiplied will be stored as ints userInput1 and userInput2 .  

Now, all we have to do is multiply the two ints and tell Java to print out the results!  Try to work it out on your own, but if you get stuck, feel free to use the code below.

Congrats!  You have created a simple calculator that works with integers values!  Be sure to save your work.  In the next lesson, review the differences between doubles and int values and how they can affect your future programs.

GitHub link.

Leave a Reply

%d bloggers like this: