5.13 Reading User Input Challenge
Hello everyone in this video, we are going to make a challenge. The challenge is first to ask - user input 10 numbers into the console and display the sum of these numbers. - Create a "Scanner" as we did in the previous video. - Use the scanner’s "hasnextint" method to check if the user has entered an int - If hasNextInt() returns false, the message is “invalid number” and continues until you have read the 10 numbers - Use the nextInt method to get each number and add it to the sum. - Use a while loop- Use a “counter” variable, which will count valid numbers. - And finally close the scanner once you no longer need it. I hope you’ve managed to make it work. So let’s break this challenge down into smaller pieces. First, we have to read 10 numbers in the console as well as count them and in addition calculate their sum. After getting 10 valid numbers, we will exit a while loop. In this case, we need to define a condition in the while loop, which checks that a user has entered 10 valid numbers. So let’s start by creating the scanner and also define the close method on the next line so that we don’t forget it.Now we will add 2 variables, counter and sum. The next step is to loop until we read 10 digits that the user entered into the console. So we’ll use the while loop. I’ll create an int variable called order and set it equal to counter plus 1