import java.util.Scanner; import java.text.DecimalFormat; public class MathTutor
ID: 3706870 • Letter: I
Question
import java.util.Scanner; import java.text.DecimalFormat; public class MathTutor f public static void main(Stringl args) f Scanner input new Scanner(System.in): System.out.printin/"Main menu"; System.out.printin/"1: Addition"): System.out.printin 2: Subtraction" System.out.println("?: Multiplication.); System.out.printin("4: Division"); System.out.println("5: Exit"): int userChoice 0 System.out.printin Enter a choice:" userChoice input.nextint): while (userChoice ! 5) int firstNum (int)(Math.random0 * 10); int secondNum- (int)/(Math.random) 10) switch (userChoice) f case 1: int adding firstNum+secondNum; System.out.printinffirstNum+++secondNum+"?") int addinginput input.nextint(): if (addinginput adding)Explanation / Answer
1. Move entire switch block inside try{} block, this will ensure program will run even if exception is thrown on any one run.
2. Add catch block following to try as shown below
catch(ArithemeticException ex){ // This will handle arithematic exceptions
System.out.println(ex.Message);
}
catch(Exception ex){ // This is to handle all exceptions
System.out.println(ex.Message);
}
Note:// Add appropriate exception packages to use this
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.