What seems fairly simple has got me completely confused. I ask the user for some inputs at the beginning of a while loop to be able to execute a program inside the loop. After the program is executed, I want to prompt the user at the end of the loop to restart or end the loop.
I am trying to make the loop ask only the LAST question after it has completed the program within it. Then, if the user has answered "yes", the loop will restart. Any other answer will result in ending the loop.
Here is my code
String rerun = null;
boolean run = true;
int x;
int y;
while (run = true)
{
// first question
x = keyboard.nextInt();
y = keyboard.nextInt();
// run program with x and y
// second question (ask to continue)
rerun = keyboard.nextLine();
if (rerun.equalsIgnoreCase("yes"))
{
continue;
}
else
{
run = false;
//break; stops the loop without answering the second question
}
}
Sample input/output:
Enter height and width of the house you want me to draw: 4 4
Program runs...
Bob, do you want me to draw another house for you (yes to continue)?
Enter height and width of the house you want me to draw (must be even numbers):
Why is it asking me the height and width again even though I have conditions at the end, prohibiting the loop to restart before I prompt it to continue?
Thank you!