Exception Handling

 

import java.util.Scanner;

public class ExceptionHandling {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);
// create two arrays
int[] array1 = { 1, 2, 3, 4, 5 };
int choice = 0;
int mathChoice = 0;
int answer = 0;

System.out.println("Welcome to the Exception Handling Program");
System.out.println("See if you can crash my program.");
do {
System.out.println("Choose one of the following options:");
System.out.println("1. Divide 36 by some number that the user enters.");
System.out.println("2. Choose an item from my array, a number between 0-4.");
System.out.println("3. Enter a number to multiply 25 by ");
System.out.println("4. Quit the program.");
choice = input.nextInt();

switch (choice) {

case 1: {
System.out.println("Enter a number to divide 36 by.");
mathChoice = input.nextInt();
answer = 36 / mathChoice;
System.out.println("36 divided by " + mathChoice + " = " + answer + ".");
break;
}

case 2: {
System.out.println("Choose an item from my array, a number between 0-4..");
mathChoice = input.nextInt();
answer = 36 / mathChoice;
System.out.println("The value found at index " + mathChoice + " is " + array1[mathChoice] + ".");
break;
}

case 3: {
System.out.println("Enter a number to multiply 25 by.");
mathChoice = input.nextInt();
answer = 25 * mathChoice;
System.out.println("25 multiplied by " + mathChoice + " = " + answer + ".");
break;
}
case 4: {
System.out.println("Thanks for using my program.");
}
} // end of switch statement
} // end of do

while (choice != 4);

input.close();
}// end of main method


} // end of class