I have a do while statement, and i want it to loop out when two conditions are met. For some reason, whenever the allUp is true, the loop ends.
public void loop(){
System.out.println(toString());
do{
turn();
System.out.println(toString());
while(allUp == false && end==false);
}
}
this:
while(allUp == false && end==false);
}
should be
} while(allUp == false && end==false);
When allUp
is true, allUp == false
is false and you exit your loop (one of the two condition is false...). it should be
while(allUp == false || end==false);
Or, as tvanfosson suggested:
while (!(allUp && end))
That looks incorrect. The while should come outside the curly brackets.
public void loop(){
System.out.println(toString());
do{
turn();
System.out.println(toString());
} while(allUp == false && end==false);
}
As mentioned in other answers, the while statement should be outside the curly brace. You might want to read over the Java Tutorials - While Loops on this topic if you are just starting out in Java programming.
Place the while statement outside the closing brace of your do clause like this:
do {
...
} while(...);
Here, you are checking the condition for the inside the while statement so that when the conditions are true, it loop again; and when false, it exits the loop.
Instead of:
do{
turn();
System.out.println(toString());
while(allUp == false && end==false);
}
Do this:
do{
turn();
System.out.println(toString());
} while(allUp == false && end==false);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.