c# - While 循环与 || 一起使用 但不是&&,不明白为什么?
问题描述
该程序的目标是掷两个骰子,直到它们都等于 6。当我在 while 语句中使用 && 时,它会在一个骰子等于 6 后停止。这是为什么呢?不应该 && 测试第一个条件然后如果它是正确的测试第二个?
while ((diceOne != 6) || (diceTwo != 6)) {
diceOne = numberGeneration.Next(1, 7);
diceTwo = numberGeneration.Next(1, 7);
attempt = ++attempt;
Console.WriteLine("Your first dice is: " + diceOne + " your second dice is: " + diceTwo);
Console.WriteLine("Press any button to continue");
Console.ReadKey();
}
Console.WriteLine("It took you " + attempt);
Console.WriteLine("Press any key to continue");
Console.ReadKey();
解决方案
它停止是因为你打破了一个骰子不同于 6 的条件。
while ((diceOne != 6) && (diceTwo != 6))
While 将在两个条件都为真时执行。
推荐阅读
- c - 在内核模块中使用 ip4 格式将网络字节顺序 IP 转换为主机字节顺序
- aurelia - 使用 Aurelia 路由器,我如何定义到同一个视图的两条路由?
- sql - 为什么在 SQL 中除以 110 / 100 会给我错误的答案?
- mysql - 如何使用链接到第三个表的条件连接两个表
- r - 使用 dplyr 查找多边形的中心
- web-scraping - 我正在尝试使用 IMPORTXML 将产品信息从 Keepa 提取到谷歌表中
- css - Mediawiki - 脚本错误:第 1 行的 Lua 错误:“/”附近出现意外符号
- nativescript - Nativescript-Vue 移除 iOS ListView 点击颜色
- database - 如何使用 VelocyPack (VPack) 在 ArangoDB 中存储图像?
- excel - 如何从数组中填充列?