控制台键盘不工作?

时间:2017-01-26 08:46:56

标签: c# console-application

所以我正在制作一个基于控制台的文字游戏,以了解更多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();
        }

自我解释。一旦它到达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... ");
        }   

似乎发生的事情是,当&#39; y&#39;或者&#39; Y&#39;被迫:

1)要求输入命中以注册上述内容。

2)它同时运行if&amp; else WriteLines,我认为它与1)一起?

如何修改我的代码,以便在&#39; y / Y&#39;按下,它读取正确的标准?或者我怎样才能不再需要按Enter键继续?

4 个答案:

答案 0 :(得分:1)

在您的代码中,在Console.WriteLine("\nTo proceed, please press any key..."); Console.ReadLine();后,您希望在控制台中输入一行(在您按Enter键之前不读取),然后在第二种方法中,您需要尝试再次读取控制台(但这次是单键)。

如果删除第一个Console.ReadLine()

,会不会有帮助

然后你在询问问题之前(以及在做另一个ReadKey之前)尝试得到是/否答案。

答案 1 :(得分:1)

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

答案 2 :(得分:1)

使用下面的代码,我测试了它并且它可以工作。

我已将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... ");
        }

答案 3 :(得分:0)

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

int age;

     

Console.Write(&#34;在输入未知之前,您能否确认您的年龄?&#34;);   age = Convert.ToInt32(Console.ReadLine());   Console.WriteLine(&#34;&#34);

可能是

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 ==&#39; y&#39; || yes ==&#39; Y&#39;)

可能是

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