将键与循环链接? C#

时间:2012-11-11 05:54:35

标签: c# console-application

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();
        }
    }


    }
}

** /

您好。我做了这个noob游戏,你按一个键来获得一个不同的菜单和一组方向。问题是,一旦我按下一个键然后转到那个特定的方法,我就不能回去了。我希望PressedKey继续循环,这样我就可以按键进入程序的不同部分。我希望我解释得这么好。

示例:我按下按键I.按键I转到指示集。我需要按退格键进入主菜单。退格不起作用。如何使这些ConsoleKeys工作?

PS:我试过,if语句,while while和while(true)。

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

感谢。

1 个答案:

答案 0 :(得分:2)

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

删除该行后,仍然会过多地调用Console.ReadKey()。每次调用该方法时,它都会输入键入的键。因此,例如,如果您按“我”,则输入此部分:

    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循环。让所有循环都由外循环完成。