While 循环中的C++ "or"语句混淆了事情

C++ "or" statement in a While loop mixes things up

本文关键字:语句 or 循环 C++ While      更新时间:2023-10-16

我面临一个小问题,当数组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]等于0,则将运行上述。如果您想停止它如果两者中的任何一个为零,您应该编写

while (!((board1[i] == 0) || (board2[i] == 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 

这个片段是错误的。您从索引0开始,检查board1或board2在该索引中是否有0,然后执行while循环,直到board1[i]!=0&amp;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
    }
}

从本质上讲,你在每次迭代时都会说"除非我有理由不这么想,否则棋盘都是零"。然后试图证明董事会并非全为零。如果其中任何一个都是零,那么什么都不要做,停止游戏。

只写而不是

while ((board1[i] == 0) || (board2[i] == 0))

如果你想写的话,当两个板需要同时为0时

while (board1[i] != 0 && board2[i] != 0)

否则,如果其中一个板需要为0才能退出,则

while (board1[i] != 0 || board2[i] != 0)