c# - While 循环与 || 一起使用但不是&&,不明白为什么?

标签 c# loops random while-loop

该程序的目标是掷两个骰子,直到它们都等于 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# - While 循环与 || 一起使用但不是&&,不明白为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66243751/

相关文章:

c# - 在 ListView 中添加/删除列

c# - 指向 PointCollection 的字符串

javascript - 加载和访问二维数组时出现问题

jQuery 在按下按钮时循环浏览图像

c# - 如果使用同一个 val 来替换两个单独的 string.Format 占位符,是否必须提供两次?

python - 如何使用Python循环删除包含特定单词的行?

c++ - 生成随机长度的随机数字串

DAG 的所有拓扑排序的随机算法?

random - cuRand Mersenne twiner __device__ 侧内核代码示例

python - 有没有一种方法可以公平地实现 Brian Kernighan 的 Python 计数算法?