Advertisement
Guest User

Untitled

a guest
Dec 21st, 2020
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. /**  
  2. * Nizar Mayraldo
  3. * 05111940000004  
  4. */
  5.  
  6. import java.util.InputMismatchException;
  7. import java.util.Scanner;
  8.  
  9. public class TryCatch {
  10.     public static int quotient(int num,int denom)
  11.         throws ArithmeticException {
  12.         return num/denom;
  13.        
  14.     }
  15.    
  16.     public static void main(String[] args) {
  17.         Scanner scanner = new Scanner(System.in);
  18.         boolean continueLoop = true;
  19.        
  20.         System.out.print("Please enter an integer numerator: ");
  21.         int numerator = scanner.nextInt();
  22.         System.out.print("Please enter an integer denominator: ");
  23.         int denominator = scanner.nextInt();
  24.        
  25.         try // read two numbers and calculate quotient
  26.         {
  27.                 int result = quotient(numerator, denominator);
  28.                 System.out.printf("%nResult: %d / %d = %d%n", numerator,
  29.                         denominator, result);
  30.                 continueLoop = false; // input successful; end looping
  31.         }
  32.         catch (InputMismatchException inputMismatchException)
  33.         {
  34.                 System.err.printf("%nException: %s%n",
  35.                         inputMismatchException);
  36.                 scanner.nextLine(); // discard input so user can try again
  37.                 System.out.printf(
  38.                         "You must enter integers. Please try again.%n%n");
  39.         }
  40.         catch (ArithmeticException arithmeticException)
  41.             {
  42.                 System.err.printf("%nException: %s%n", arithmeticException);
  43.                 System.out.printf(
  44.                         "Zero is an invalid denominator. Please try again.%n%n");
  45.             }
  46.         //if program works normal
  47.         System.out.println("Program selesai");
  48.         scanner.close();
  49.  
  50.     }
  51.  
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement