如何使用 C# 在控制台中生成 Yes/No 提示符?

How would I make a Yes/No prompt in Console using C#?

我是 C# 的新手,我一直在编写代码来练习。

我已经编写了一些代码,您可以在其中输入一行文本或一个 "login key",我想将其添加到您可以确认登录密钥的位置。

这是我到目前为止完成的代码,我将在卡住的部分使用“//”。 (不会包含任何不必要的代码。)

    Console.WriteLine("Please enter a login key.");

    string Key = Console.ReadLine();

    Console.WriteLine("You entered, " + Key + " as your login key!");

    Console.WriteLine("Are you sure you want to choose this as your login key? "); // This is where I'm stuck at, got no idea how to do a yes/no prompt.
}

如有任何帮助,我们将不胜感激。

您可以使用 while 循环

bool confirmed = false;

while(!confirmed)
{
    Console.WriteLine("Please enter a login key.");
    string Key = Console.ReadLine();
    Console.WriteLine("You entered, " + Key + " as your login key!");
    Console.WriteLine("Are you sure you want to choose this as your login key? [yes/no]");

    string option = Console.ReadLine();

    if (option == "yes")
    {
        confirmed = true;
    }

}

这样如果 option 不是 yes 它将保留在 while 循环中。

使用Console.ReadKey();

Console.WriteLine("Press 'y' if you would like awesome!");

ConsoleKeyInfo cki =    Console.ReadKey();

if (cki.Key.ToString() == "y")
{
        //do what you need for yes
}else {
     // presses something other then Y
}

您很容易过度使用这些控制台登录脚本。这是我的贡献:

bool confirmed = false;
string Key;
do {
    Console.Write("Please enter a login key: ");
    Key = Console.ReadLine();
    Console.WriteLine("You entered, " + Key + " as your login key!");

    ConsoleKey response;
    do
    {
        Console.Write("Are you sure you want to choose this as your login key? [y/n] ");
        response = Console.ReadKey(false).Key;   // true is intercept key (dont show), false is show
        if (response != ConsoleKey.Enter)
            Console.WriteLine();

    } while (response != ConsoleKey.Y && response != ConsoleKey.N);

    confirmed = response == ConsoleKey.Y;
} while (!confirmed);
Console.WriteLine("You chose {0}!", Key);
Console.ReadLine();

示例输出:

Please enter a login key: potato
You entered, potato as your login key!
Are you sure you want to choose this as your login key? [y/n] a
Are you sure you want to choose this as your login key? [y/n] s
Are you sure you want to choose this as your login key? [y/n] l
Are you sure you want to choose this as your login key? [y/n] d
Are you sure you want to choose this as your login key? [y/n] k
Are you sure you want to choose this as your login key? [y/n] f
Are you sure you want to choose this as your login key? [y/n] d
Are you sure you want to choose this as your login key? [y/n] n
Please enter a login key: banana
You entered, banana as your login key!
Are you sure you want to choose this as your login key? [y/n] y
You chose banana!

我更愿意创建一个可重用的方法来处理所有未来的问题。非常简化的版本来说明基础知识。

void Main(string[] args)
{
    if(PromptConfirmation("Did you answer yes?"))
    {
        //todo: Handle positive response
    }
}

private bool PromptConfirmation(string confirmText)
{
    Console.Write(confirmText + " [y/n] : ");
    ConsoleKey response = Console.ReadKey(false).Key;
    Console.WriteLine();
    return (response == ConsoleKey.Y);
}

我像实用程序一样重新制作它class我希望它会有用:

class UtilsConsole
{
        public static bool Confirm(string title)
        {
            ConsoleKey response;
            do
            {
                Console.Write($"{ title } [y/n] ");
                response = Console.ReadKey(false).Key;  
                if (response != ConsoleKey.Enter)
                {
                    Console.WriteLine();
                }
            } while (response != ConsoleKey.Y && response != ConsoleKey.N);

            return (response == ConsoleKey.Y);
        }
}