控制台按键不起作用?

Console Keypress not working?

所以我正在制作一个基于控制台的文字游戏来学习更多 C#,但我现在被困住了并求助于 SO。

这可能是一个显而易见的答案,但我需要另一双眼睛来帮助我。我已经检查了这里的其他问题,但它们似乎对我没有帮助。

在我的 Main() 中,我有以下内容:

        int age;

        Console.Write("Before entering the unknown, could you please confirm your age? ");
        age = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("");

        if (age < 18)
        {
            Console.WriteLine("I'm sorry, you'll now be transferred to a non-explicit version.");
            Console.WriteLine("\n<Press any key to continue>");
            Console.ReadKey();
            Console.Write("\nPlease wait...");
            for (int t = 5; t >= 0; t--)
            {
                Console.CursorLeft = 22;
                Console.Write("{0:00}" + " seconds remaining", t);
                Thread.Sleep(1000);
            }
            Console.WriteLine("");
            Console.WriteLine("\nTo proceed, please press any key...");
            Console.ReadLine();

            NonExplicit();
        }

不言自明。一旦它命中 NonExplicit() 方法,将调用以下内容:

        char yes = Console.ReadKey().KeyChar;
        string name;

        Console.WriteLine("\nYou're awake? It's about time. Not old enough, eh? That sucks. \n\nListen, I really need your help. Would you be interested? Y / N");
        Console.ReadKey();

        if (yes == 'y' || yes == 'Y')
        {
            Console.Write("\nYou will?! Wow, That's fantastic. Right then, where shall I start? \nI know! How about telling me your name? ");
            name = Console.ReadKey().ToString();
            Console.WriteLine("\nAhh yes, now you mention it, I certainly remember you, {0}!", name);   
        }
        else
        {
            Console.WriteLine("\nYou don't want to help me? Oh, that's unfortunate... ");
        }   

似乎发生的是,当按下 'y' 或 'Y' 时:

1) 需要回车才能注册以上内容

2) 它同时运行 ifelse WriteLines,我假设它与 1)?

一起运行

如何修改我的代码,以便在按下 'y/Y' 时读取正确的条件?或者我怎样才能消除按下 Enter 键才能继续的需要?

char yes = Console.ReadKey(); // No need for .KeyChar

int age;

Console.Write("Before entering the unknown, could you please confirm your age? "); age = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("");

可能是

int age = -1
Console.WriteLine("Before entering the unknown, could you please confirm your age? ");
while (age == -1)
{
    if !(int.TryParse(Console.ReadLine(), out age))
    {
        Console.WriteLine("Please enter a valid age");
    }
}

name = Console.ReadKey().ToString();

应该是

name = Console.ReadLine();

if (yes == 'y' || yes == 'Y')

可能是

if (yes.ToUpper() == "Y")

在你的代码中,在 Console.WriteLine("\nTo proceed, please press any key..."); 之后你有 Console.ReadLine(); 期望你在控制台中输入一行(直到你按下回车键才读取)然后在你的第二种方法中,你是尝试再次读取控制台(但这次是一个键)。

如果删除第一个 Console.ReadLine() 会有帮助吗?

然后你试图在问你的问题之前获得 yes/no 答案(并且在做另一个 ReadKey 之前。

string result = Console.ReadLine();
if (result.ToUpper().Equals("Y"))
  {
     //any thing
  }

在下面使用了您的代码,我对其进行了测试并且有效。

我将 char yes = Console.ReadKey() 移到了 Console.WriteLine() 下面。
我将 name = Console.ReadKey() 更改为 Console.ReadLine() 因为名称多于 1 个键。

        string name;
        Console.WriteLine("\nYou're awake? It's about time. Not old enough, eh? That sucks. \n\nListen, I really need your help. Would you be interested? Y / N");
        char yes = Console.ReadKey();

        if (yes == 'y' || yes == 'Y')
        {
            Console.Write("\nYou will?! Wow, That's fantastic. Right then, where shall I start? \nI know! How about telling me your name? ");
            name = Console.ReadLine();
            Console.WriteLine("\nAhh yes, now you mention it, I certainly remember you, {0}!", name);
            Console.ReadKey(); // I put this here so the program wouldn't exit
        }
        else
        {
            Console.WriteLine("\nYou don't want to help me? Oh, that's unfortunate... ");
        }