【问题标题】:Constraining user input to one of a specified set of values将用户输入限制为一组指定值之一
【发布时间】:2020-03-31 23:08:30
【问题描述】:

(我已经尝试找到类似的问题但没有运气,所以我希望也许有人可以提供解决方案)

我在第一堂课中有一个 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();

我的单独值类输入法:

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();

我尝试告诉它在 convert 不是米、英尺或码时继续循环,但在抛出参数后我无法继续程序。

我能否在抛出此 System.ArgumentException 后继续我的应用程序?如果可以,我将在我的 while 循环中输入什么来允许这样做?

【问题讨论】:

  • 附注:Console.ReadLine 已经返回一个字符串。无需对返回的值调用 Convert.ToString。 (事实上​​,Convert.ToString 会将null 转换为"",这将阻止您检测输入结束,例如,如果标准输入被重定向。)

标签: c# console-application


【解决方案1】:

问题是你调用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();

【讨论】:

    【解决方案2】:

    这个Input() 函数非常狂野。它到底在做什么?基本上什么都没有。你想返回一些确定的值来表明它是什么类型的单元,对吧?

    让我们用这个替换它:

    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();
    

    【讨论】:

    • 对于任何其他类似的项目,我一定会考虑到这一点,只是一个问题: (input.ToLowerInvariant() 行在做什么?(我假设它只是从大写转换小写?但据我所知,这只是 .ToLower() 对吧?)
    • 是的,它正在转换为小写,但您应该始终考虑输入的字符集。这是一个有助于解释的好问题:stackoverflow.com/questions/6225808/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多