Reputation: 13
Let me start off by saying that I am a relatively newer coder and have little to no experience, I'm sorry if my questions may be easy to some people. I need to write a program that uses a while loop that will ask the user a question and if they have a certain number or above it, they will get a certain response, if not, they are told to try it again. I think I've done most of it right but everytime I put in the correct number, it doesn't break immediately and loops back one time before stopping.
public class TaffyTester
{
public static void main(String[] args)
{
System.out.println("Starting the Taffy Timer...");
System.out.print("Enter the temperature: ");
while (true)
{
Scanner input = new Scanner(System.in);
int temp = input.nextInt();
System.out.println("The mixture isn't ready yet.");
System.out.print("Enter the temperature: ");
if (temp >= 270)
{
System.out.println("Your taffy is ready for the next step!");
break;
}
}
}
}
Upvotes: 1
Views: 59
Reputation: 44844
I would change the order so it is more logical
System.out.println("Starting the Taffy Timer...");
Scanner input = new Scanner(System.in);
int temp = 0;
while (temp < 270)
{
System.out.println("The mixture isn't ready yet.");
System.out.print("Enter the temperature: ");
temp = input.nextInt();
}
System.out.println("Your taffy is ready for the next step!");
Do things in the order that makes more sense. Make the while
dependdant on your condition. Things that are to be done after the condition is met should be done after the loop.
Result
Starting the Taffy Timer...
The mixture isn't ready yet.
Enter the temperature: 800
Your taffy is ready for the next step!
Upvotes: 2