Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Nizar Mayraldo
- * 05111940000004
- */
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class TryCatch {
- public static int quotient(int num,int denom)
- throws ArithmeticException {
- return num/denom;
- }
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- boolean continueLoop = true;
- System.out.print("Please enter an integer numerator: ");
- int numerator = scanner.nextInt();
- System.out.print("Please enter an integer denominator: ");
- int denominator = scanner.nextInt();
- try // read two numbers and calculate quotient
- {
- int result = quotient(numerator, denominator);
- System.out.printf("%nResult: %d / %d = %d%n", numerator,
- denominator, result);
- continueLoop = false; // input successful; end looping
- }
- catch (InputMismatchException inputMismatchException)
- {
- System.err.printf("%nException: %s%n",
- inputMismatchException);
- scanner.nextLine(); // discard input so user can try again
- System.out.printf(
- "You must enter integers. Please try again.%n%n");
- }
- catch (ArithmeticException arithmeticException)
- {
- System.err.printf("%nException: %s%n", arithmeticException);
- System.out.printf(
- "Zero is an invalid denominator. Please try again.%n%n");
- }
- //if program works normal
- System.out.println("Program selesai");
- scanner.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement