Hi, I am currently building a Menu driven calculator, and i have got my loop to go, but now when the user enters " 6 " from the menu - the program does not terminate. How can i get this to end?
I also tried using a " Break;" but it didnt seem to end anything
import java.util.Scanner; public class MenuDrivenCalculator { // Main menu public static void main(String[] args) { // Create Scanner Scanner input = new Scanner(System.in); int invalid = 0; while (invalid <= 3) { System.out.println(" Menu: "); System.out.println("1. Add "); System.out.println("2. Sub "); System.out.println("3. Multiply "); System.out.println("4. Divide "); System.out.println("5. Generate a Random Number"); System.out.println("6. Quit"); // Complete the input on what you would like to do. System.out.println(" What would you like to do?"); int menuNumber = input.nextInt(); if (menuNumber < 1) { System.out.println("Im Sorry " + menuNumber + " is not an option on the menu"); invalid = invalid + 1; } if (menuNumber > 6) { System.out.println("Im Sorry " + menuNumber + " is not an option on the menu"); invalid = invalid + 1; } if (menuNumber == 1) { System.out.println("What is the first Number?:"); int number1 = input.nextInt(); System.out.println("What is the Second Number?:"); int number2 = input.nextInt(); int answer = number1 + number2; System.out.println("Answer : " + answer); invalid = 0; } if (menuNumber == 2) { System.out.println("What is the first Number?:"); int number1 = input.nextInt(); System.out.println("What is the Second Number?:"); int number2 = input.nextInt(); int answer = number1 - number2; System.out.println("Answer : " + answer); invalid = 0; } if (menuNumber == 3) { System.out.println("What is the first Number?:"); int number1 = input.nextInt(); System.out.println("What is the Second Number?:"); int number2 = input.nextInt(); int answer = number1 * number2; System.out.println("Answer : " + answer); invalid = 0; } if (menuNumber == 4) { System.out.println("What is the first Number?:"); int number1 = input.nextInt(); System.out.println("What is the Second Number?:"); int number2 = input.nextInt(); if (number2 != 0) { System.out.println(number1 / number2); // Divides first number by second number } else if (number2 == 0) { System.out.println("I'm sorry, you cannot divide by zero."); invalid = 0; } if (menuNumber == 5) { System.out.println("What is the lower limit?:"); int min = input.nextInt(); System.out.println("What is the upper limit?:"); int max = input.nextInt(); int answer = (int) (Math.ceil(Math.random() * (max - min)) + min); System.out.println("Answer: " + answer); invalid = 0; } if (menuNumber == 6) { System.exit(0); } } } } }