作者:手机用户2502875355 | 来源:互联网 | 2022-12-10 03:39
我试图使用while循环进行检查我希望循环只要求用户重新输入其值,如果触发了三个条件之一.即如果响应为空,也不是"Y"或"N".我这样做是通过使用!运营商.我注意到即使响应是正确的选择,while循环仍然要求重新输入一个值.我也注意到,当我删除!操作员从第二个条件开始,用户在循环块工作后输入正确的响应代码,但是当我添加!操作员返回到循环工作的条件,即使响应是正确的.
PromptMessage("If you are using a different download path for your mods enter (Y)es. Or if you want to exit out the" +
" program enter (N)o!", ConsoleColor.Green);
string CustomPath = Console.ReadLine();
CustomPath.ToUpper();
Console.WriteLine(CustomPath);
while (!CustomPath.Contains("Y") || !CustomPath.Contains("N") || String.IsNullOrEmpty(CustomPath))
{
AlertMessage("Please enter either Y to continue or N to exit");
CustomPath = Console.ReadLine();
CustomPath.ToUpper();
}
maccettura..
5
你这里有几个错误.首先,字符串在C#中是不可变的,所以这样做:
string foo = "some string";
foo.ToUpper();
意味着在运行之后foo
仍然等于"some string"
.您需要将值赋给变量(它甚至可以是相同的变量).像这样:
string foo = "some string";
foo = foo.ToUpper();
//foo = "SOME STRING"
下一个问题是你的循环和逻辑.我认为更简单的方法是使用do/while
循环并在while条件下检查输入的"有效性".该do/while
循环是指检查之前,你将永远"做"一旦出while
状况.你总是想要输入一次,所以使用这个循环更有意义:
public static void Main()
{
//defined in outer scope
string customPath = string.Empty;
do
{
Console.WriteLine("If you are using a different download path for your mods enter (Y)es. Or if you want to exit out the program enter (N)o!");
//Calling ToUpper() before assigning the value to customPath
customPath = Console.ReadLine().ToUpper();
}
while (customPath != "N" && customPath != "Y");
}
我在这里做了一个小提琴
1> maccettura..:
你这里有几个错误.首先,字符串在C#中是不可变的,所以这样做:
string foo = "some string";
foo.ToUpper();
意味着在运行之后foo
仍然等于"some string"
.您需要将值赋给变量(它甚至可以是相同的变量).像这样:
string foo = "some string";
foo = foo.ToUpper();
//foo = "SOME STRING"
下一个问题是你的循环和逻辑.我认为更简单的方法是使用do/while
循环并在while条件下检查输入的"有效性".该do/while
循环是指检查之前,你将永远"做"一旦出while
状况.你总是想要输入一次,所以使用这个循环更有意义:
public static void Main()
{
//defined in outer scope
string customPath = string.Empty;
do
{
Console.WriteLine("If you are using a different download path for your mods enter (Y)es. Or if you want to exit out the program enter (N)o!");
//Calling ToUpper() before assigning the value to customPath
customPath = Console.ReadLine().ToUpper();
}
while (customPath != "N" && customPath != "Y");
}
我在这里做了一个小提琴