Java - Program does not exit the loop?

while (choice != 7) { System.out.println("--- Mathematical Calculator ---"); System.out.println(""); System.out.println("Pick an operation from the list - Use nos. 1 to 7"); System.out.println("1) Multiplication"); System.out.println("2) Division"); System.out.println("3) Addition"); System.out.println("4) Subtraction"); System.out.println("5) Find the area of a regular object"); System.out.println("6) Find the volume of a regular object"); System.out.println("7) Exit"); choice = userInput.nextInt(); switch (choice) { case 1: { System.out.println(""); System.out.println("You have chosen multiplication"); System.out.println("Enter a number"); double num1 = userInput.nextDouble(); System.out.println("Enter another number"); double num2 = userInput.nextDouble(); double num3 = num1 * num2; System.out.println(num1 + " * " + num2 + " = " + num3); num1 = num3; while (choice2 != 5 || choice2 != 6) { System.out.println(""); System.out.println(""); System.out.println("If you would like to build on the answer obtained - pick an operation from the list - Use nos. 1 to 4"); System.out.println("Else press 5 to return to the main menu"); System.out.println("1) Multiplication"); System.out.println("2) Division"); System.out.println("3) Addition"); System.out.println("4) Subtraction"); System.out.println("5) Start new calculation"); System.out.println("6) Exit"); choice2 = userInput.nextInt(); switch (choice2) { case 1: { System.out.println("Enter number"); num2 = userInput.nextDouble(); num3 = num1 * num2; System.out.println(num1 + " * " + num2 + " = " + num3); num1 = num3; break; } case 2: { System.out.println("Enter number"); num2 = userInput.nextDouble(); num3 = num1 / num2; System.out.println(num1 + " / " + num2 + " = " + num3); num1 = num3; reak; } case 3: { System.out.println("Enter number"); num2 = userInput.nextDouble(); num3 = num1 + num2; System.out.println(num1 + " + " + num2 + " = " + num3); num1 = num3; break; } case 4: { System.out.println("Enter number"); num2 = userInput.nextDouble(); num3 = num1 - num2; System.out.println(num1 + " - " + num2 + " = " + num3); num1 = num3; break; } case 5: choice = 0; break; case 6: choice = 7; break; default: System.out.println("Invalid choice"); } choice2 = 0; } break; } 

I posted a short piece of my code. My problem is that when I entered 5 or 6 in the second query ( choice2 ), my program continues the loop, and not exit the loop and return to the main menu / end the program.

Would thank for some feedback on what I'm doing wrong.

+4
source share
2 answers

Your while (choice2 != 5 || choice2 != 6) incorrect, since it will always be true. If choice2 is 5, then the second sentence will be true.

Not to mention that you always set choice2 to 0 at the end of the loop, so the loop will go on forever, even if you replace || at && .

+6
source

Condition choice2 != 5 || choice2 != 6 choice2 != 5 || choice2 != 6 always true because there is no number equal to 5 and 6 at the same time.

If you want to exit the loop when 5 or 6 is entered, use && instead:

 while (choice2 != 5 && choice2 != 6) 

Make sure choice2 set before continuing the next iteration of the loop.

Note that you can exit the loop from your switch with the marked break construct:

 calc: // Label the loop for using labeled break while (true) { ... switch(...) { case 1: break; // This will exit the switch ... case 6: break calc; // This will exit the loop } } 
+3
source

All Articles