简体   繁体   English

在do-while循环中同时检查两个条件

[英]checking two conditions simultaneously in a do-while loop

In a tic-tac toe code, I had a do-while loop that checks if one of the players have won..so, something like this 在tic-tac脚趾代码中,我有一个do-while循环来检查其中一个玩家是否赢了..所以,这样的事情

do{
//does some player's input and other things..
}while(!win(x));

now, the big problem is in this loop, it will continue looping until one of the players win. 现在,最大的问题是在这个循环中,它会继续循环直到其中一个玩家获胜。 now, how do i check a tie using this same do-while loop? 现在,我如何使用相同的do-while循环检查平局?

could I do something like this : 我可以这样做:

do{
 //still the same checking
}while(!win(x)||!loose(x));

I did try this but it just messed up the code. 我试过这个,但它只是搞砸了代码。 How could I possibly check a tie in the game? 我怎么可能在比赛中找到平局?

Thanks 谢谢

Your logic is slightly off - change the loop condition from: 你的逻辑略有偏离 - 改变循环条件:

do{
 //still the same checking
}while(!win(x)||!loose(x));

to: 至:

do{
 //still the same checking
}while(!win(x)&&!loose(x));

or perhaps a more easily understood, but equivalent alternative: 或者更容易理解,但等效的替代方案:

do{
 //still the same checking
}while(!(win(x)||loose(x)));

When you are writing: 在你写作时:

!win(x)||!loose(x)

You say not won or not lost and the loop will terminate in the first go. 你说没赢或不输,循环将在第一次结束时终止。 You can use the following: 您可以使用以下内容:

do{
    //still the same checking
} while (!win(x)&&!loose(x));

or 要么

do{
    //still the same checking
} while (!(win(x)||loose(x)));   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM