C# 控制台按键不工作?

C# 控制台按键不工作?,c#,console-application,C#,Console Application,所以我制作了一个基于控制台的文本游戏来学习更多的C#,但我现在陷入困境,不得不求助于它 这可能是一个显而易见的答案,但我需要另一双眼睛来帮助我。我已经检查了这里的其他问题,它们似乎对我没有帮助 在我的Main()中,我有以下内容: int age; Console.Write("Before entering the unknown, could you please confirm your age? "); age = Convert.ToI

所以我制作了一个基于控制台的文本游戏来学习更多的C#,但我现在陷入困境,不得不求助于它

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

在我的
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();
        }
当按下“y”或“y”时,似乎会发生以下情况:

1) 需要点击enter键才能注册上述内容

2) 它同时运行
if
else
WriteLines
,我假设它与1)一起运行

我如何修改代码,以便在按下“y/y”时读取正确的标准?或者,我如何取消按Enter键继续操作的需要

char yes = Console.ReadKey(); // No need for .KeyChar
智力年龄

控制台。写下(“在进入未知领域之前,您能确认您的年龄吗?”); age=Convert.ToInt32(Console.ReadLine()); 控制台。写线(“”)

可能是

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");
    }
}
if (yes.ToUpper() == "Y")
name=Console.ReadKey().ToString()

应该是

name = Console.ReadLine();
如果(是=='y'|是=='y')

可能是

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");
    }
}
if (yes.ToUpper() == "Y")

在您的代码中,
Console.WriteLine(\n要继续,请按任意键…)您有
Console.ReadLine()
它希望您在控制台中输入一行(在按enter键之前不读取),然后在第二个方法中,您尝试再次读取控制台(但这次是针对单个键)

如果删除第一个
控制台.ReadLine()
,是否会有所帮助


然后,在提问之前(以及在进行另一个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... ");
        }

char yes=Console.ReadKey().KeyChar;
然后是一条
WriteLine
,后面是另一条
Console.ReadKey();
?尝试移动
char yes=Console.ReadKey().KeyChar;
以替换第二条
ReadKey()
。这非常有效。谢谢大家的帮助。没问题,谢谢你们提供了一个有效的代码片段:)这就是为什么我们都可以帮助你们的原因。