开发者

Do While statement - Two arguments

I have a do while statement, 开发者_如何学JAVAand 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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜