如何制作一个用户必须输入完整选项的有效菜单?

时间:2018-07-17 12:51:05

标签: c# loops if-statement switch-statement user-input

我正在做一个菜单屏幕,用户需要在其中输入确切的选项之一。目前,我正在这样做:

static void Start()
    {
        Console.WriteLine("Options: " + Environment.NewLine);
        string[] options = { "Option 1", "Option 2", "Option 3" };
        foreach (string value in options)
        {
            Console.WriteLine(value);
        }
        Console.Write("Type the option you want: ");
        string choosen = Console.ReadLine();
        if(choosen == "Option 1")
        {
            Console.WriteLine(Environment.NewLine + "Your choosen option was Option 1" + Environment.NewLine);
            Start();
        }
        else if (choosen == "Option 2")
        {
            Console.WriteLine(Environment.NewLine + "Your choosen option was Option 2" + Environment.NewLine);
            Start();
        }
        else if(choosen == "Option 3")
        {
            Console.WriteLine(Environment.NewLine + "Your choosen option was Option 3" + Environment.NewLine);
            Start();
        }
        else
        {
            Console.WriteLine(Environment.NewLine + "Please choose a valid option!" + Environment.NewLine);
            Start();
        }
    }

我可以说这不是一种非常有效的方法,但是我不知道其他任何方法。

我知道我可以这样做:

ConsoleKeyInfo key = Console.ReadKey();
        switch (key.Key)
        {
            case ConsoleKey.D1:
                Console.WriteLine(Environment.NewLine + "Your choosen option was Option 1" + Environment.NewLine);
                Start();
                break;
            case ConsoleKey.D2:
                Console.WriteLine(Environment.NewLine + "Your choosen option was Option 2" + Environment.NewLine);
                Start();
                break;
            case ConsoleKey.D3:
                Console.WriteLine(Environment.NewLine + "Your choosen option was Option 3" + Environment.NewLine);
                Start();
                break;
            default:
                Console.WriteLine(Environment.NewLine + "Please choose a valid option!" + Environment.NewLine);
                Start();
                break;
        }

但是我希望用户完全写一个选项,例如“ Option 1”左右。这样,用户需要按一个键。

那么,有没有一种更有效的方式来执行此操作,或者仅出于此确切目的,我只能按照自己的方式进行操作?我真的不喜欢为我拥有的每个选项都使用if-else语句。

1 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情...

public class Option
{
    public string Description { get; }
    public string Method { get; }

    public Option(string description, string method)
    {
        Description = description;
        Method = method;
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        var options = new[]
        {
            new Option("1 to print \"Hello\".", "PrintHello"),
            new Option("2 to print \"World\".", "PrintWorld")
        };

        Console.WriteLine("Please press the number of the desired option:");
        foreach (var option in options)
        {
            Console.WriteLine($"{option.Description}");
        }

        char key;

        while (true)
        {
            key = Console.ReadKey().KeyChar;
            if (key >= '1' && key <= '0' + options.Length)
            {
                break;
            }
            Console.WriteLine($"{Environment.NewLine}Please choose an option from 1 to {options.Length}");
        }

        Console.WriteLine($"{Environment.NewLine}You selected option {key}");

        var selected = options[key - '1'];

        typeof(Program).GetMethod(selected.Method).Invoke(null, null);

        // This line is just to stop the console window closing
        Console.ReadLine();
    }

    public static void PrintHello()
    {
        Console.WriteLine("Hello");
    }

    public static void PrintWorld()
    {
        Console.WriteLine("World");
    }
}

很明显,这仅适用于最多9个选项。另外,如果要调用的方法不是静态的,则可能需要有所不同。如果是这样,请告诉我,我可以提供另一个示例。