Please help with my while loop that turned into infinite loop!
I wrote the code below for class assignment was to create program that does a calculation based on unput from menu and would continue to loop until user inputed exit number. But when compiled instead of prompting menu again it goes into an infinite loop i think. Please any help is appreciated. I believe the break is not working properly not sure if I am using it right.
Code Java:
import java.util.Scanner;
public class Calculator
{
public static void main(String[] args)
{
int choice, choice2, choice3;
float result;
Scanner scan = new Scanner (System.in);
System.out.println("What would you like to do? ");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Exit");
System.out.print("Selection: ");
choice = scan.nextInt();
if (choice == 5)
{
System.out.println("Maybe next time try a calculation.");
}
else
{
System.out.print("Enter first number: ");
choice2 = scan.nextInt();
System.out.print("Enter second number: ");
choice3 = scan.nextInt();
do
{
switch (choice)
{
case 1:
//Add 2 integers
System.out.println(choice2 + "+" + choice3 + "=" + (choice2 + choice3));
break;
case 2:
//Subtract 2 integers
System.out.println(choice2 + "-" + choice3 + "=" + (choice2 - choice3));
break;
case 3:
//Multiply 2 integers
System.out.println(choice2 + "*" + choice3 + "=" + (choice2 * choice3));
break;
case 4:
//Divide 2 integers
result = choice2 / choice3;
System.out.println(choice2 + "/" + choice3 + "=" + result);
break;
case 5:
//Exit
default:
//
break;
}
}while (choice != 5);
}
}
}
Re: Please help with my while loop that turned into infinite loop!
in your code you don't need do-while loop there.. remove loop and try.. its working..
Re: Please help with my while loop that turned into infinite loop!
Hello Hazmat210,
the do...while loop is in the else block which means that choice doesn't equal to 5 and so the statement choice != 5 is always true and that's why your loop is infinite. For an assignment like yours a do...while loop is appropriate but i think you should work a little more with your logic.
Re: Please help with my while loop that turned into infinite loop!
Thank you for the replies the program should go like this:
Display five options for a calculation
Ask for selection
First integer
Second integer
if 5 is selected program terminates.
else perform calculation with integers
Display equation and answer
Then repeat selection menu again until 5 is chosen.
The reason i put the first selection in the if else statement was so that if 5 was chosen then the program could terminate otherwise it prompts for the first and second integers.
Re: Please help with my while loop that turned into infinite loop!
I initially wrote the code without the if else statement and the loop was still infinite it just keep displaying the equation until I closed the program. But I can see how the if else statement makes it infinite. It's the actual while loop that I am having trouble with it doesn't break or if it is it doesn't go back to the menu selection which is where i want it to go.
Re: Please help with my while loop that turned into infinite loop!
If you want the code to loop back to the menu part, you will have to put that inside of the loop.
Re: Please help with my while loop that turned into infinite loop!
How do I do that? I thought that's what the break was for in the switch.
Re: Please help with my while loop that turned into infinite loop!
The break in the switch is for the switch to prevent falling through to the next case. It causes execution to exit the switch statement.
begin loop
menu
do stuff
end loop
Re: Please help with my while loop that turned into infinite loop!
Ok, so i need to include the menu inside the while statement I see I will post new code here as soon as I get a chance I really do appreciate the help.
Re: Please help with my while loop that turned into infinite loop!
So thank you to all who replied. Big thank you too Norm I guess I really need to sit down sketch out the logic and then write the code.
Here is the rewritten working code.
Code java:
import java.util.Scanner;
public class Calculator
{
public static void main(String[] args)
{
int choice, choice2, choice3;
float result;
Scanner scan = new Scanner (System.in);
do
{
System.out.println("What would you like to do? ");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Exit");
System.out.print("Selection: ");
choice = scan.nextInt();
System.out.print("Enter first number: ");
choice2 = scan.nextInt();
System.out.print("Enter second number: ");
choice3 = scan.nextInt();
switch (choice)
{
case 1:
//Add 2 integers
System.out.println(choice2 + "+" + choice3 + "=" + (choice2 + choice3));
break;
case 2:
//Subtract 2 integers
System.out.println(choice2 + "-" + choice3 + "=" + (choice2 - choice3));
break;
case 3:
//Multiply 2 integers
System.out.println(choice2 + "*" + choice3 + "=" + (choice2 * choice3));
break;
case 4:
//Divide 2 integers
result = choice2 / choice3;
System.out.println(choice2 + "/" + choice3 + "=" + result);
break;
case 5:
//Exit
default:
//
break;
}
}while (choice != 5);
}
}