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.
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
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
Excellent, we have the input we need! Both of the numbers that the user wants multiplied will be stored as ints
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.
Leave a Reply