-2

I am trying to do a simple program where the user selects a number 1,2,3 for a simple calculation. The program will continue to run unless the user types in '0' when asked on the menu. The same menu will be presented to the user as long as they select 1, 2 or 3.If the user was to select 0, the program will terminate.

The code I have:

import java.util.Scanner;
 public class QuadraticSolving {

public static void main(String[] args) {
    // Declarations
    int user;
    double a;
    double b;
    double c;
    int num1;
    int num2;
    Scanner in = new Scanner(System.in);// Create scanner in
    do{
        System.out.print("Press 0 to exit, 1 for Multiplication Solving, 2 for Sum or 3 for Print Message:");// main menu
        user = in.nextInt();
    }
   while(user == 0);
    {  
        if(user == 1)// multiplication solver
        {
            System.out.print("Enter a:");
            a = in.nextDouble();
            System.out.print("Enter b:");
            b = in.nextDouble();
            System.out.print("Enter c:");
            c = in.nextDouble();
            System.out.println(a*b*c);
        }
        else if(user == 2)// addition
        {
            System.out.print("Enter an integer:");
            num1 = in.nextInt();
            System.out.print("Enter another integer:");
            num2 = in.nextInt();
            System.out.println(num1 + num2);
           // System.out.println(allSum(num1,num2));
            System.out.print("");
        }
        else if (user == 3)// welcome statement
        {
            System.out.println("Welcome to IT");
        }
        else
        {
            System.out.println("Bye");
            break;
        }
        System.out.print("Press 0 to exit, 1 for Multiplication Solving, 2 for Sum or 3 for Print Message:");
        user = in.nextInt();     
    }
}

After the first run through the menu, it works perfectly. During the 2nd run, it always terminates the program, no matter what choice you pick.

Outcome example here

Whenever I enter '0' for all the runs it turns into an endless loop. The only way I can stop it is by ending the execution of the code. Endless example here

I don't understand why the program only runs perfectly the first time and the 2nd run it terminates on its own. Also, I don't get why it goes into an endless loop when the user enters '0'. Any explanation as to why this happens? (Sorry for bothering, I am new to programming. Thanks)

  • 5
    `do { ... } while(...);` is the loop. The block of code after it is not part of any loop, regardless of the braces. – kaya3 Jan 21 '20 at 22:31
  • 4
    `do ... while` (and every other loop) aborts if condition is `false`. Also, you may want your big code block after `do`. – CoderCharmander Jan 21 '20 at 22:31
  • There are several problems, including the mis-placed braces *AFTER* the actual loop. Regarding `Scanner.next()`, look here: [Java: Infinite loop using Scanner in.hasNextInt()](https://stackoverflow.com/questions/1794281/java-infinite-loop-using-scanner-in-hasnextint) and here [Scanner.hasNextInt()](https://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html#hasNextInt%28%29) – FoggyDay Jan 21 '20 at 22:35
  • The whole if-else statement is within the while loop. – Anand Pandey Jan 21 '20 at 22:35
  • You should try thoroughly debugging before posting these types of questions. A breakpoint placed in your do-while or if-else loop would've likely answered the question for you. Otherwise, the other comments are correct – Tyler Dickson Jan 21 '20 at 22:36
  • "_The whole if-else statement is within the while loop_" – you don't have a while loop, only a do-while loop. – Slaw Jan 21 '20 at 22:37

1 Answers1

0

It enters in a endless loop when the user types 0 because the while expression is checking if the typed number is equal to 0 and not different

And it is running ok on the first time because the code that runs after the while is not repeating, it only executes once, that code should be inside here

do {
    .
    . // Here you scan
    . // Here you paste the logic
    .
} while (user != 0);
Felipe Malara
  • 1,263
  • 1
  • 4
  • 11