C#:while 与 if
C#: while versus if
我只想接受州的两个大写字母缩写:AK、WA、OR、CA、AZ、NM、CO 或 UT。如果输入不是其中之一,则请求用户提供新的输入以进行更正。我下面的代码用于过滤结果,但是当我输入“UT”或“WA”时,它不会退出 while 循环并继续。
Write("State (We only ship to AK, WA, OR, CA, AZ, NM, CO, or UT):\t");
stateName = Convert.ToString(ReadLine().ToUpper());
while (!stateName.Contains("AK") || !stateName.Contains("WA") || !stateName.Contains("OR")
|| !stateName.Contains("CA") || !stateName.Contains("AZ") || !stateName.Contains("NM")
|| !stateName.Contains("CO") || !stateName.Contains("UT"))
{
Write("Error: We do not ship to {0}. We ONLY ship to AK, WA, OR, CA, AZ, NM, CO, or UT\t", stateName);
stateName = Convert.ToString(ReadLine().ToUpper());
}
进一步修改后,我得到了它:
Write("State (We only ship to AK, WA, OR, CA, AZ, NM, CO, or UT):\t");
stateName = Convert.ToString(ReadLine().ToUpper());
if (stateName != "AK" || stateName != "WA" || stateName != "OR"
|| stateName != "CA" || stateName != "AZ" || stateName != "NM"
|| stateName != "CO" || stateName != "UT")
{
Write("Error: We do not ship to {0}. We ONLY ship to AK, WA, OR, CA, AZ, NM, " +
"CO, or UT\t", stateName);
stateName = Convert.ToString(ReadLine().ToUpper());
}
我的问题是,如果逻辑非常相似,为什么 while 循环不起作用? !variableName
与 variableName != value
不同吗?
问题出在你的逻辑上,你应该使用 And &&
运算符而不是 Or ||
:
while (
!stateName.Contains("AK") && !stateName.Contains("WA") && !stateName.Contains("OR")
&& !stateName.Contains("CA") && !stateName.Contains("AZ") && !stateName.Contains("NM")
&& !stateName.Contains("CO") && !stateName.Contains("UT"))
我假设您正在学习,所以我使用了这段代码。在现实世界中,您应该使用数组来检查如此多的条件(您需要知道 for
循环和 Array
)。
我只想接受州的两个大写字母缩写:AK、WA、OR、CA、AZ、NM、CO 或 UT。如果输入不是其中之一,则请求用户提供新的输入以进行更正。我下面的代码用于过滤结果,但是当我输入“UT”或“WA”时,它不会退出 while 循环并继续。
Write("State (We only ship to AK, WA, OR, CA, AZ, NM, CO, or UT):\t");
stateName = Convert.ToString(ReadLine().ToUpper());
while (!stateName.Contains("AK") || !stateName.Contains("WA") || !stateName.Contains("OR")
|| !stateName.Contains("CA") || !stateName.Contains("AZ") || !stateName.Contains("NM")
|| !stateName.Contains("CO") || !stateName.Contains("UT"))
{
Write("Error: We do not ship to {0}. We ONLY ship to AK, WA, OR, CA, AZ, NM, CO, or UT\t", stateName);
stateName = Convert.ToString(ReadLine().ToUpper());
}
进一步修改后,我得到了它:
Write("State (We only ship to AK, WA, OR, CA, AZ, NM, CO, or UT):\t");
stateName = Convert.ToString(ReadLine().ToUpper());
if (stateName != "AK" || stateName != "WA" || stateName != "OR"
|| stateName != "CA" || stateName != "AZ" || stateName != "NM"
|| stateName != "CO" || stateName != "UT")
{
Write("Error: We do not ship to {0}. We ONLY ship to AK, WA, OR, CA, AZ, NM, " +
"CO, or UT\t", stateName);
stateName = Convert.ToString(ReadLine().ToUpper());
}
我的问题是,如果逻辑非常相似,为什么 while 循环不起作用? !variableName
与 variableName != value
不同吗?
问题出在你的逻辑上,你应该使用 And &&
运算符而不是 Or ||
:
while (
!stateName.Contains("AK") && !stateName.Contains("WA") && !stateName.Contains("OR")
&& !stateName.Contains("CA") && !stateName.Contains("AZ") && !stateName.Contains("NM")
&& !stateName.Contains("CO") && !stateName.Contains("UT"))
我假设您正在学习,所以我使用了这段代码。在现实世界中,您应该使用数组来检查如此多的条件(您需要知道 for
循环和 Array
)。