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.
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)