簡體   English   中英

在do-while循環中同時檢查兩個條件

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

在tic-tac腳趾代碼中,我有一個do-while循環來檢查其中一個玩家是否贏了..所以,這樣的事情

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

現在,最大的問題是在這個循環中,它會繼續循環直到其中一個玩家獲勝。 現在,我如何使用相同的do-while循環檢查平局?

我可以這樣做:

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

我試過這個,但它只是搞砸了代碼。 我怎么可能在比賽中找到平局?

謝謝

你的邏輯略有偏離 - 改變循環條件:

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

至:

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

或者更容易理解,但等效的替代方案:

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

在你寫作時:

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

你說沒贏或不輸,循環將在第一次結束時終止。 您可以使用以下內容:

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

要么

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM