Advertisement
luliu

Chapter 12 Project 71039

Mar 23rd, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. /*
  2.  * Name: Lu Liu
  3.  * Date: 3/23/2016
  4.  * Course Number: CSC-112
  5.  * Course Name: Intermediate Topics in Java Programming
  6.  * Email: lliu0001@student.stcc.edu
  7.  *
  8.  * Assignment: HW # 10
  9.  * Programe Description:
  10.  * MyProgrammingLab assignment for Chapter 12 Project 71039
  11.  * Write a program  that prompts the user to read two integers  and displays their sum
  12.  */
  13.  
  14. package MyProgramming;
  15. import java.util.*;
  16.  
  17. public class TestNumber {
  18.     public static void main(String[] args) {
  19.         Scanner sc = new Scanner(System.in);
  20.         int n1 = 0;
  21.         int n2 = 0;
  22.         while (true) {
  23.             try {
  24.                 System.out.print("Enter first integer: ");
  25.                 n1 = sc.nextInt();
  26.                 break;
  27.             } catch (InputMismatchException e) {
  28.                 System.out.println("Please enter an integer.");
  29.                 sc.nextLine();
  30.             }
  31.         }
  32.         while (true) {
  33.             try {
  34.                 System.out.print("Enter second integer: ");
  35.                 n2 = sc.nextInt();
  36.                 break;
  37.             } catch (InputMismatchException e) {
  38.                 System.out.println("Please enter an integer.");
  39.                 sc.nextLine();
  40.             }
  41.         }
  42.         System.out.println("The sum of " + n1 + " and " + n2 + " is " + (n1 + n2));
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement