gpt4 book ai didi

c# - 陷入无限 if 循环

转载 作者:太空狗 更新时间:2023-10-30 01:16:58 26 4
gpt4 key购买 nike

我正在 Visual Studio 2015 中为我的类(class)开发一个 C# 控制台骰子模拟实验室。我根据用户的响应将我的程序分解为三个 if 循环。如果用户未输入有效响应,我有一个专用的 do while 循环。由于某种原因,我陷入了那个循环,无法摆脱。最重要的是,因为它是告诉用户输入有效的响应,所以它是第一个 if 语句。因此,即使我输入“y”、“Y”、“n”或“N”,它仍然会初始化。这是有问题的部分。

        // get response from user
response = ReadLine();

// loop starts if user does not reply with valid response in order to retrieve a valid response
if (response != "N" || response != "Y" || response != "n" || response != "y")
{
do
{
WriteLine("Please reply with Y or N");
response = ReadLine();
}
while (response != "N" || response != "Y" || response != "n" || response != "y");
}

我正在使用 or 运算符,所以我不明白为什么它会这样循环。

最佳答案

response != "N" || response != "Y" || response != "n" || response != "y"

应该是

response != "N" && response != "Y" && response != "n" && response != "y"

因为如果您点击其中一个有效响应,您应该退出循环

关于c# - 陷入无限 if 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33429896/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com