C++ 即使只满足一个条件,if 语句仍然为真
C++ The if statement is still true even when only one condition is met
我正在用 C++ 创建一个井字游戏程序,快完成了,我遇到的问题是游戏需要以平局结束。我创建了一个 if-statement
来检查何时选择了特定组合,即使只满足一个条件,它也会发现它为真。
这是代码...
//takes the users selections and sees if all spots are filled in without a match.
if((b1== "O" || "X") && (b2 == "O" || "X") && (b3 == "O" || "X")){
cout << "The game ended in a tie.";
// Flip = 3 ends the game in a tie.
flip = 3;
}
例如...当我将 `b1` 更改为和 `"O"` 或 `"X"` 时游戏以平局结束,即使 `b2` 和 `b3` 仍然是空的。
条件:
b1== "O" || "X"
评估为:
(b1=="O") || "X"
这总是正确的。你可能想要:
(b1=="O" || b1=="X")
我正在用 C++ 创建一个井字游戏程序,快完成了,我遇到的问题是游戏需要以平局结束。我创建了一个 if-statement
来检查何时选择了特定组合,即使只满足一个条件,它也会发现它为真。
这是代码...
//takes the users selections and sees if all spots are filled in without a match.
if((b1== "O" || "X") && (b2 == "O" || "X") && (b3 == "O" || "X")){
cout << "The game ended in a tie.";
// Flip = 3 ends the game in a tie.
flip = 3;
}
例如...当我将 `b1` 更改为和 `"O"` 或 `"X"` 时游戏以平局结束,即使 `b2` 和 `b3` 仍然是空的。
条件:
b1== "O" || "X"
评估为:
(b1=="O") || "X"
这总是正确的。你可能想要:
(b1=="O" || b1=="X")