Question
I need my code to be able to catch exceptions and allow the user to retry the answers and not just send you back to
I need my code to be able to catch exceptions and allow the user to retry the answers and not just send you back to the start or just go out of program all together. And also if there are anymore exceptions that can be used to add them also. My code is just catching the exception and exiting the code instead of letting user retry the answer again. Last person did not correct my code for what i asked. I need the user to be able to try to same question again not try another question.
import java.util.*; import java.text.DecimalFormat;
public class Lab_7 { private static Scanner input; public static void main(String[] args) { try { input = new Scanner(System.in); System.out.println("Main menu"); System.out.println("1. Addition"); System.out.println("2. Subtraction"); System.out.println("3. Multiplication"); System.out.println("4. Division"); System.out.println("5. Exit"); int userChoice = 0; System.out.println("Enter a choice:"); userChoice = input.nextInt(); while(userChoice != 5) { int firstNum = (int)(Math.random() * 10); int secondNum = (int)(Math.random() * 10); switch(userChoice) { case 1: // Making addition code int adding = firstNum + secondNum; System.out.println(firstNum + " + " + secondNum + "!"); int addingInput = input.nextInt(); if(addingInput == adding) System.out.println("correct"); else System.out.println("Your answer is wrong. The correct answer is " + adding); break; case 2: // Making subtraction code int sub = firstNum - secondNum; System.out.println(firstNum + " - " + secondNum + "!"); int subInput = input.nextInt(); if(subInput == sub) System.out.println("correct"); else System.out.println("Your answer is wrong. The correct answer is " + sub); break; case 3: // Multiplication code int mult = firstNum * secondNum; System.out.println(firstNum + " * " + secondNum + "!"); int mulInput = input.nextInt(); if(mulInput == mult) System.out.println("correct"); else System.out.println("Your answer is wrong. The correct answer is " + mult); break; case 4: while(secondNum == 0) { //to make sure that bottom number is never zero secondNum = (int)Math.round(Math.random() * 10); } double div = firstNum / secondNum; // Division System.out.println(firstNum + " / " + secondNum + "!"); double divInput = input.nextDouble(); if(divInput == div) System.out.println("correct"); else System.out.println("Your answer is wrong. The correct answer is " + div); break; case 5: System.exit(0); break; default: // Any number past 5 is out of range System.out.println("Error: Out of range"); System.exit(0); } System.out.println("Enter a choice: "); userChoice = input.nextInt(); } System.out.println("Thank you for using the Math Program. Have a Great Day!"); } //Handling InputMismatchException catch(InputMismatchException ex) { System.out.println("Error!!! Invalid input..."); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started