将用户输入限制为一组指定值中的一个
Constraining user input to one of a specified set of values
(我试过找到类似的问题,但没有成功,所以我希望有人能提供解决方案)
我在我的第一个 class 中有一个 do while 循环,目标是循环直到用户输入英尺、米或码,如下所示:
string convert = "";
do
{
Console.Clear();
Console.WriteLine("What conversion: Feet, Meters, Yards");
try
{
convert = Convert.ToString(Console.ReadLine());
}
catch (Exception)
{
Console.Clear();
Console.WriteLine("Incorrect conversion");
Console.ReadKey();
}
convert = Values.Input(convert);
Console.WriteLine(convert);
Console.ReadKey();
} while (_____); // Trying to loop while argument is thrown from my other class
Console.WriteLine("Continue on");
Console.ReadLine();
我的独立值classes 输入法:
public static string Input(string input)
{
string convert = input;
if (convert == "Meters")
{
input = "Meters";
}
else if (convert == "Feet")
{
input = "Feet";
}
else if (convert == "Yards")
{
input = "Yards";
}
else
{
throw new System.ArgumentException("Incorrect conversion");
//Trying to loop my main program if an ArgumentException is thrown here
}
return input;
}
我尝试过的:
} while (convert != "Meters" || convert != "Feet" || convert != "Yards");
Console.WriteLine("Continue on");
Console.ReadLine();
我试着告诉它在转换不是米、英尺或码时继续循环,但在抛出参数后我无法继续程序。
在抛出这个 System.ArgumentException
之后我能继续我的申请吗?如果可以,我会在我的 while 循环中输入什么来允许这个?
问题是你调用Values.Input()
的位置在try/catch语句之外,当这抛出异常时,它没有在你定义的catch中处理。所以它会被调用堆栈赶上。尝试将 Values.Input(..)
放在 try/catch 语句中。
string convert = "";
do
{
Console.Clear();
Console.WriteLine("What conversion: Feet, Meters, Yards");
try
{
convert = Convert.ToString(Console.ReadLine());
// ------- PASTE
convert = Values.Input(convert);
Console.WriteLine(convert);
// -----
}
catch (Exception)
{
Console.Clear();
Console.WriteLine("Incorrect conversion");
}
// XXXXXXXXX CUT
// XXXXXXXXX
Console.ReadKey();
} while (_____); //Trying to loop while argument is thrown from my other class
Console.WriteLine("Continue on");
Console.ReadLine();
这个 Input()
函数非常疯狂。它到底在做什么?基本上没什么。你想要 return 一些确定的值来指示它是什么类型的单位,对吧?
让我们将其替换为:
public enum Unit { Meters, Feet, Yards }
public static Unit Input(string input)
{
switch (input.ToLowerInvariant())
case "meters": return Unit.Meters;
case "feet": return Unit.Feet;
case "yards": return Unit.Yards;
default: throw new System.ArgumentException("Incorrect conversion");
}
}
所以之后我们可以修复代码:
Unit unit;
while (true)
{
Console.WriteLine("What conversion: Feet, Meters, Yards");
try
{
var input = Console.ReadLine();
unit = Values.Input(convert);
break; // if we get here we didn't throw an exception, so we can exit the loop
}
catch
{
Console.WriteLine("Incorrect conversion");
}
}
Console.WriteLine("Continue on");
Console.ReadLine();
(我试过找到类似的问题,但没有成功,所以我希望有人能提供解决方案)
我在我的第一个 class 中有一个 do while 循环,目标是循环直到用户输入英尺、米或码,如下所示:
string convert = "";
do
{
Console.Clear();
Console.WriteLine("What conversion: Feet, Meters, Yards");
try
{
convert = Convert.ToString(Console.ReadLine());
}
catch (Exception)
{
Console.Clear();
Console.WriteLine("Incorrect conversion");
Console.ReadKey();
}
convert = Values.Input(convert);
Console.WriteLine(convert);
Console.ReadKey();
} while (_____); // Trying to loop while argument is thrown from my other class
Console.WriteLine("Continue on");
Console.ReadLine();
我的独立值classes 输入法:
public static string Input(string input)
{
string convert = input;
if (convert == "Meters")
{
input = "Meters";
}
else if (convert == "Feet")
{
input = "Feet";
}
else if (convert == "Yards")
{
input = "Yards";
}
else
{
throw new System.ArgumentException("Incorrect conversion");
//Trying to loop my main program if an ArgumentException is thrown here
}
return input;
}
我尝试过的:
} while (convert != "Meters" || convert != "Feet" || convert != "Yards");
Console.WriteLine("Continue on");
Console.ReadLine();
我试着告诉它在转换不是米、英尺或码时继续循环,但在抛出参数后我无法继续程序。
在抛出这个 System.ArgumentException
之后我能继续我的申请吗?如果可以,我会在我的 while 循环中输入什么来允许这个?
问题是你调用Values.Input()
的位置在try/catch语句之外,当这抛出异常时,它没有在你定义的catch中处理。所以它会被调用堆栈赶上。尝试将 Values.Input(..)
放在 try/catch 语句中。
string convert = "";
do
{
Console.Clear();
Console.WriteLine("What conversion: Feet, Meters, Yards");
try
{
convert = Convert.ToString(Console.ReadLine());
// ------- PASTE
convert = Values.Input(convert);
Console.WriteLine(convert);
// -----
}
catch (Exception)
{
Console.Clear();
Console.WriteLine("Incorrect conversion");
}
// XXXXXXXXX CUT
// XXXXXXXXX
Console.ReadKey();
} while (_____); //Trying to loop while argument is thrown from my other class
Console.WriteLine("Continue on");
Console.ReadLine();
这个 Input()
函数非常疯狂。它到底在做什么?基本上没什么。你想要 return 一些确定的值来指示它是什么类型的单位,对吧?
让我们将其替换为:
public enum Unit { Meters, Feet, Yards }
public static Unit Input(string input)
{
switch (input.ToLowerInvariant())
case "meters": return Unit.Meters;
case "feet": return Unit.Feet;
case "yards": return Unit.Yards;
default: throw new System.ArgumentException("Incorrect conversion");
}
}
所以之后我们可以修复代码:
Unit unit;
while (true)
{
Console.WriteLine("What conversion: Feet, Meters, Yards");
try
{
var input = Console.ReadLine();
unit = Values.Input(convert);
break; // if we get here we didn't throw an exception, so we can exit the loop
}
catch
{
Console.WriteLine("Incorrect conversion");
}
}
Console.WriteLine("Continue on");
Console.ReadLine();