C# While语句忽略响应值
我有一个命令行要求用户说(Y/N),这个值被传递给checkResponse方法 出于某种原因,while循环忽略了该值,即使在调试时它显示的值是“Y”。当该值设置为“N”时,它还会继续循环。如果我将If语句移到while语句下面,程序将运行一半。我可以发送一个初始值“Y”,while语句将忽略它并开始运行其中的代码 你知道我遗漏了什么还是找过头了吗 提前谢谢C# While语句忽略响应值,c#,command-line,if-statement,while-loop,console-application,C#,Command Line,If Statement,While Loop,Console Application,我有一个命令行要求用户说(Y/N),这个值被传递给checkResponse方法 出于某种原因,while循环忽略了该值,即使在调试时它显示的值是“Y”。当该值设置为“N”时,它还会继续循环。如果我将If语句移到while语句下面,程序将运行一半。我可以发送一个初始值“Y”,while语句将忽略它并开始运行其中的代码 你知道我遗漏了什么还是找过头了吗 提前谢谢 public void checkResponse(string response, string confirmValue) {
public void checkResponse(string response, string confirmValue)
{
Console.WriteLine(response);
Console.WriteLine(response);
if (response == "Y")
{
return;
}
else if (response == "N")
{
Environment.Exit(0);
}
else
{
while ((response != "Y") || (response != "N"))
{
Console.Clear();
Console.WriteLine("\"" + response + "\" is not a valid response.");
Console.WriteLine();
Console.WriteLine("You entered:" + confirmValue);
Console.WriteLine("Is this correct? (Y/N)");
response = Console.ReadLine().ToUpper();
}
}
}
从
或
更改为和
逻辑运算符:
(response != "Y") && (response != "N")
从
或
更改为和
逻辑运算符:
(response != "Y") && (response != "N")
将始终为真,因为响应不能同时为Y和N
将始终为真,因为响应不能同时为Y和N。您的
((响应!=“Y”)|(响应!=“N”)
始终为真您的((响应!=“Y”)|(响应!=“N”)
始终为真确定。如果而循环终止怎么办?我们想让它再次检查:
public void checkResponse(string response, string confirmValue)
{
Check:
Console.WriteLine(response);
Console.WriteLine(response);
if (response == "Y")
{
return;
}
else if (response == "N")
{
Environment.Exit(0);
}
else
{
while ((response != "Y") && (response != "N"))
{
Console.Clear();
Console.WriteLine("\"" + response + "\" is not a valid response.");
Console.WriteLine();
Console.WriteLine("You entered:" + confirmValue);
Console.WriteLine("Is this correct? (Y/N)");
response = Console.ReadLine().ToUpper();
}
goto Check;
}
}
或者更好:
public void checkResponse(string response, string confirmValue)
{
Console.WriteLine(response);
while (response != "Y" && response != "N")
{
Console.Clear();
Console.WriteLine("\"" + response + "\" is not a valid response.");
Console.WriteLine();
Console.WriteLine("You entered:" + confirmValue);
Console.WriteLine("Is this correct? (Y/N)");
response = Console.ReadLine().ToUpper();
}
if (response == "N")
Environment.Exit(0);
}
嗯。如果而循环终止怎么办?我们想让它再次检查:
public void checkResponse(string response, string confirmValue)
{
Check:
Console.WriteLine(response);
Console.WriteLine(response);
if (response == "Y")
{
return;
}
else if (response == "N")
{
Environment.Exit(0);
}
else
{
while ((response != "Y") && (response != "N"))
{
Console.Clear();
Console.WriteLine("\"" + response + "\" is not a valid response.");
Console.WriteLine();
Console.WriteLine("You entered:" + confirmValue);
Console.WriteLine("Is this correct? (Y/N)");
response = Console.ReadLine().ToUpper();
}
goto Check;
}
}
或者更好:
public void checkResponse(string response, string confirmValue)
{
Console.WriteLine(response);
while (response != "Y" && response != "N")
{
Console.Clear();
Console.WriteLine("\"" + response + "\" is not a valid response.");
Console.WriteLine();
Console.WriteLine("You entered:" + confirmValue);
Console.WriteLine("Is this correct? (Y/N)");
response = Console.ReadLine().ToUpper();
}
if (response == "N")
Environment.Exit(0);
}
您确定输入也是大写的吗?您可以使用该方法并忽略该案例
message.Equals("Y", StringComparison.InvariantCultureIgnoreCase);
编辑:虽然这可能会更好地处理带有或而不是and的错误,但这是您的实际问题 您确定输入也是大写的吗?您可以使用该方法并忽略该案例
message.Equals("Y", StringComparison.InvariantCultureIgnoreCase);
编辑:虽然这可能会更好地处理带有或而不是and的错误,但这是您的实际问题 我做了更多的研究,最后用while(!response.equals(“Y”))做了更多的研究,最后用while(!response.equals(“Y”))做了更多的研究,现在我大声说出来,这很有意义。谢谢你指出这一点。现在我大声说出来,这很有道理。谢谢你指出。是的,我被它弄糊涂了,我让它在进入循环之前写出值。是的,我被它弄糊涂了,我让它在进入循环之前写出值。