0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameEdition3
{
class Program
{
    static void Main(string[] args)
    {

        Data classData = new Data();

        Console.WriteLine("{0}", classData.TitleMenu());

        while (true) 
        {
            ConsoleKeyInfo PressKey;
            PressKey = Console.ReadKey();

            while (PressKey.Key == ConsoleKey.Backspace)
            {
                Console.Clear();
                Console.WriteLine("{0}", classData.TitleMenu());
                PressKey  = Console.ReadKey();
            }



            while (PressKey.Key == ConsoleKey.I)
            {
                Console.Clear();
                Console.WriteLine("{0}", classData.Information());
                PressKey =Console.ReadKey();
            }

            while (PressKey.Key == ConsoleKey.D1)
            {
                Console.Clear();
                Console.Write("Please type in the Item you want. Warhammer, Heavy Armor, Boots, or Sword: ");
                Console.WriteLine("{0}", classData.myFunction());
                Console.WriteLine("\nPress Backspace to go back to the menu and you can view your item in the Display all Items tab");
                PressKey = Console.ReadKey();
            }

            while (PressKey.Key == ConsoleKey.D2)
            {
                Console.Clear();
                Console.WriteLine("{0}", classData.result);
                PressKey = Console.ReadKey();


            }

            while (PressKey.Key == ConsoleKey.D3)
            {
                Console.Clear();
                PressKey = Console.ReadKey();

            }


            if (PressKey.Key == ConsoleKey.D4)
            {
                return;

            }

            Console.Read();
        }
    }


    }
}

**/

你好。我制作了这个菜鸟游戏,您可以在其中按下一个键来获得不同的菜单和一组方向。问题是,一旦我按下一个键并转到那个特定的方法,我就无法返回。我希望 PressedKey 继续循环,这样我就可以按键进入程序的不同部分。我希望我解释得足够好。

示例:我按 I 键。 I 键进入一组方向。我需要按退格键才能进入主菜单。退格不起作用。如何使这些 ConsoleKeys 工作?

PS:我尝试了 while、if 语句、do while 和 while(true)。

如果有人想尝试一下,这是它附带的类: http : //pastebin.com/GivANrwC 将类命名为 Data.cs。

谢谢。

4

1 回答 1

2

一个问题是Console.Read();循环末尾的行。这告诉程序等待输入一行文本,然后按 Enter 键。键入的文本的第一个字母没有保存在任何地方并且丢失了

删除那条线后,您仍然调用Console.ReadKey()太多。每次调用该方法时,它都会吃掉输入的键。因此,例如,如果您按“I”,则进入此部分:

    while (PressKey.Key == ConsoleKey.I)
    {
        Console.Clear();
        Console.WriteLine("{0}", classData.Information());
        PressKey =Console.ReadKey();
    }

之后WriteLine,它会调用ReadKey(),如果不是ConsoleKey.I,则退出小循环,然后重新启动大循环。但是在更大的循环开始时,您ReadKey()再次调用,而不检查最后按下的键的值。

试试这个版本:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameEdition3
{
class Program
{
    static void Main(string[] args)
    {

        Data classData = new Data();

        Console.WriteLine("{0}", classData.TitleMenu());

        while (true) 
        {
            ConsoleKeyInfo PressKey;
            PressKey = Console.ReadKey();

            if (PressKey.Key == ConsoleKey.Backspace)
            {
                Console.Clear();
                Console.WriteLine("{0}", classData.TitleMenu());
            }
            else if (PressKey.Key == ConsoleKey.I)
            {
                Console.Clear();
                Console.WriteLine("{0}", classData.Information());
            }
            else if (PressKey.Key == ConsoleKey.D1)
            {
                Console.Clear();
                Console.Write("Please type in the Item you want. Warhammer, Heavy Armor, Boots, or Sword: ");
                Console.WriteLine("{0}", classData.myFunction());
                Console.WriteLine("\nPress Backspace to go back to the menu and you can view your item in the Display all Items tab");
            }
            else if (PressKey.Key == ConsoleKey.D2)
            {
                Console.Clear();
                Console.WriteLine("{0}", classData.result);
            }
            else if (PressKey.Key == ConsoleKey.D3)
            {
                Console.Clear();
            }
            else if (PressKey.Key == ConsoleKey.D4)
            {
                return;
            }
        }
    }


}
}

while这也表明,当您检查按下了哪个键时,无需使用小循环。让所有的循环都由外循环完成。

于 2012-11-11T06:44:00.413 回答