“while”循环中的“C++”或“语句”将事物混合起来。

“while”循环中的“C++”或“语句”将事物混合起来。,c++,while-loop,C++,While Loop,我面临一个小问题,当阵列board1[I]或board2[I]仅包含0时,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 < ca

我面临一个小问题,当阵列board1[I]或board2[I]仅包含0时,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时,我想要 循环也停止

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;
        }



    }
我想你写的与你想要的恰恰相反:

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 
这一部分是错误的。从索引0开始,检查board1或board2在该索引中是否有0,然后执行while循环,直到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
    }
}

从本质上讲,在每次迭代中,你都会说,直到我有理由不这样想,所有的电路板都是零。然后试图证明董事会并非都是零。如果其中一个都是零,那就什么都不要做,停止比赛

如果在board1[i]==0 | | board2[i]==0时写入,则循环将在board1或board2等于0时运行。如果希望循环在有0时停止,则在board1[i]时停止0 | |板2[i]!=0这是错误的,当两个数组都不等于0时,您已放下。他要求一个或另一个数组,不一定两个都要。你说当0将终止循环时,其中一些数组将终止循环。如果你写,那么两块板必须有0。。