RGAlex

Reading User Input Challenge

Apr 17th, 2021
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.69 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class ReadingUserInputChallenge {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int counter = 0, sum = 0;
  7.         while (counter < 10) {
  8.             int order = counter + 1;
  9.             System.out.println("Enter number #" + order + ":");
  10.             boolean isAnInt = scanner.hasNextInt();
  11.             if (isAnInt) {
  12.                 int number = scanner.nextInt();
  13.                 counter++;
  14.                 sum += number;
  15.             } else System.out.println("Invalid number");
  16.             scanner.nextLine();
  17.         }
  18.         System.out.println("Sum = " + sum);
  19.         scanner.close();
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment