Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 限制用户输入y或n,并相应地在C中显示结果#_C#_Loops_Boolean Logic - Fatal编程技术网

C# 限制用户输入y或n,并相应地在C中显示结果#

C# 限制用户输入y或n,并相应地在C中显示结果#,c#,loops,boolean-logic,C#,Loops,Boolean Logic,我正在编写一个代码,询问用户的姓名,以及他是否想读一个笑话。答案应限于y或n。任何其他字符都应被视为无效,并应返回到问题。这是我到目前为止所做的 static void Main(string[] args) { string name=""; string KeyChar; Console.WriteLine("Hello, what is your name?"); name=Console.ReadLine(

我正在编写一个代码,询问用户的姓名,以及他是否想读一个笑话。答案应限于y或n。任何其他字符都应被视为无效,并应返回到问题。这是我到目前为止所做的

    static void Main(string[] args)
    {
        string name="";
        string KeyChar;

        Console.WriteLine("Hello, what is your name?");
        name=Console.ReadLine();

        Console.WriteLine("Greetings, " + name + ". Would you like to read a joke? (y/n)");

        ConsoleKeyInfo result = Console.ReadKey();
        Console.WriteLine("\n");

        while (true)
        {

            while ((result.KeyChar != 'Y') || (result.KeyChar != 'y') || (result.KeyChar != 'N') || (result.KeyChar != 'n'))
            {
                Console.WriteLine("Invalid key. Limit your answer to y or n");
                break;
            }

            while ((result.KeyChar == 'Y') || (result.KeyChar == 'y') || (result.KeyChar == 'N') || (result.KeyChar == 'n'))
            {
                if ((result.KeyChar == 'Y') || (result.KeyChar == 'y'))
                {
                    Console.WriteLine("Joke question?");
                    Console.ReadLine();
                    Console.WriteLine("Joke answer.");
                    break;
                }
                else if ((result.KeyChar == 'N') || (result.KeyChar == 'n'))
                {
                    Console.WriteLine("You're missing on all the fun! Suit yourself. Goodbye!");
                    break;
                }

            }
        }

    }

您最好编写一个单独的方法来请求Y或N,例如:

public static char YorN(string prompt)
{
    Console.Write(prompt + " ");

    while (true)
    {
        char input = char.ToUpper(Console.ReadKey().KeyChar);

        if (input == 'Y' || input == 'N')
        {
            Console.WriteLine();
            return input;
        }

        Console.Write("\b \b"); // Use backspace ('\b') to erase the incorrect character.
    }
}
这样就不需要在循环中调用它:

Console.WriteLine("Hello, what is your name?");
string name = Console.ReadLine();

char response = YorN("Greetings, " + name + ". Would you like to read a joke? (y/n)");

if (response == 'Y')
{
    Console.WriteLine("Joke question?");
    Console.ReadLine();
    Console.WriteLine("Joke answer.");
}
else
{
    Console.WriteLine("You're missing on all the fun! Suit yourself. Goodbye!");
}
结果=Console.ReadKey();在while结束时(true),用ifs替换你的2个内部while。