[英]C++ “or” statement in a While loop mixes things up
我面臨一個小問題,當數組board1[i]
或board2[i]
僅包含0時,While循環不會停止。
所以寫while ((board1[i] == 0) || (board2[i] == 0))
是正確的,因為我想要的是某些板僅包含0時我希望循環也停止。
void ShootAtShip(int board1[], int board2[], string names[], int cap){
const int hit = 0;
int shot = 0;
for (int i = 0; i < cap; i++){
while ((board1[i] == 0) || (board2[i] == 0)){ //detects if any board has all their ships shot down
cout << names[1] << " set a position to shoot." << endl;
cin >> shot;
while ((shot >= cap) || (shot < 0)) { //detects if the number is allowed
cout << "That number is not allowed, "<< names[1] << " set a position to shoot." << endl;
cin >> shot;
}
if (board1[shot] != 0){
board1[shot] = 0;
cout << "Hit!" << endl;
}
else{
cout << "You missed." << endl;
}
shot = 0;
cout << names[0] << " set a position to shoot." << endl;
cin >> shot;
while ((shot >= cap) || (shot < 0)) { //detects if the number is allowed
cout << "That number is not allowed, " << names[0] << " set a position to shoot." << endl;
cin >> shot;
}
if (board2[shot] != 0){
board2[shot] = 0;
cout << "Hit!" << endl;
}
else{
cout << "You missed." << endl;
}
}
我想要的是某些板僅包含0時,我希望循環也停止。
我想您寫的恰恰與您想要的相反:
while ((board1[i] == 0) || (board2[i] == 0))
如果board1 [i]等於零,或者如果board2 [i]等於零 , 則將執行上述操作。 如果您想在兩者之一為零時停止它,則應該寫
while (!((board1[i] == 0) || (board2[i] == 0)))
注意! (不是)開頭。 同樣,你也可以寫
while (board1[i] != 0 && board2[i] != 0)
只是寫而不是
while ((board1[i] == 0) || (board2[i] == 0))
如果您希望兩個板同時需要為0,請寫
while (board1[i] != 0 && board2[i] != 0)
否則,如果其中一個董事會需要為0退出,則
while (board1[i] != 0 || board2[i] != 0)
您要說的是,如果任一板的零位繼續前進。 您要說的是,直到一個董事會沒有董事會為止。
for (int i = 0; i < cap; i++){
while ((board1[i] == 0) || (board2[i] == 0)){ //ACTUALLY DETECTS IF EITHER
//BOARD HAS A ZERO AT INDEX i
這部分是錯誤的。 您從索引零開始,檢查是否EITHER board1或board2在該索引中為0,然后執行while循環填充UNTIL board1 [i]!= 0 && board2 [i]!=0。這意味着您一旦找到零索引就永遠不會停止循環,這意味着游戲不會結束。
您需要做的是改為分別遍歷每個數組,看看是否滿足條件,然后決定要做什么。 親身? 我會在兩個陣列中尋找一個。 在這兩個數組中並非全為零。 這樣,您就無需尋找停止的理由。 讓您的程序變得懶惰:除非有理由繼續,否則請停止。
遵循以下內容,看其中一個是否全為零:
bool boardOneAllZeroes = false;
bool boardTwoAllZeroes = false;
//So if board one is all zeroes OR board two is all zeroes, stop looping.
while(!boardOneAllZeroes && !boardTwoAllZeroes)
{
boardOneAllZeroes = true;
boardTwoAllZeroes = true;
//The above two lines basically say "This loop isn't going to keep
//going unless you give me a good reason to later on.
//Next we go through each index in both arrays looking for a one.
for(int i = 0; i < cap; i++)
{
//If we find a one in board one, then board one is not all zeroes.
//Set it back to false
if(board1[i] == 1)
{
boardOneAllZeroes = false;
}
//Same thing with board two.
if(board2[i] == 1)
{
boardTwoAllZeroes = false;
}
}
//Check to make sure both still have a one, because we don't need to keep going
//if both are all zeros.
if(!boardOneAllZeroes && !boardTwoAllZeroes)
{
do game things
}
}
從本質上講,您首先在每次迭代中都說“直到我有理由認為否則,木板都是零”。 然后設法證明木板不是全零。 然后,如果其中一個全為零,則不執行任何操作並停止游戲。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.