While 循环中的C++ "or"语句混淆了事情
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]等于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&;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)
相关文章:
- 我如何将 OR 与 if 语句一起使用字符串
- 将 if 语句中的字符与 or 进行比较
- C++ 虽然语句不适用于字符串和"or"
- 在 if 语句中处理多个 or 的更优雅的方法是什么
- C++ if 语句将条件设置为 true 和循环中的 OR-ing 条件之间的行为差异
- 如何生成 OR like 语句编译时?C++
- OR 语句执行所有评估
- 将 OR 放入 if 语句会导致 ISO C++禁止指针和整数之间的比较(f 允许)
- 如何在 else-if 语句中正确使用 or 语句 (||)
- 带有 or 表达式的 If 语句中的语法错误
- 涉及while循环和OR语句的新手C++问题
- While 循环中的C++ "or"语句混淆了事情
- C++开关语句不允许我使用多个"or"?什么?
- if语句检查每个OR操作符
- if语句在有两个OR选项的第三个条件下不起作用
- 在or条件语句中是否存在强制对两个表达式求值的模式或技巧?
- IF语句中的多个OR或AND条件
- c++模板编程中的Or语句
- 为什么我不能使用 ||OR C++ Switch 语句中的运算符?
- "if"语句"or"运算符不起作用