c++ - C++ if 语句在只满足一个条件时仍然为真
问题描述
我正在用 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# - 将整数列表写入文件的最快方法
- java - 大小为 n 的所有可能的 k 个列表组合
- javascript - 如何避免 React 表单中的数据重复
- .net - 使用 .net 的项目中的 TimeScaleData
- vhdl - 需要 VHDL 代码解释 (std_logic_vector)
- java - Eclipse:安装失败并出现错误
- regex - Python 正则表达式模式没有找到我所期望的
- json - OpenStack Ubuntu 18.4 中的 JSONDecodeError
- oracle - 在 Oracle 中取最大相关值?
- path - 如何更改 react-app-rewired 的构建路径