2017-04-21 51 views
-1

我期待让菜单循环,所以当我进入帮助菜单,然后返回到主菜单,它将实际运行。相反,当输入任何内容时它都不响应。退出也无论如何不起作用,因此,我将不胜感激任何帮助。谢谢。如何让菜单循环

using System; 

namespace MasterMind 
{ 
    class Menu 
    { 
     public void DrawMainMenu() 
     { 
      Console.ForegroundColor = ConsoleColor.Green; 
      Console.WriteLine("    MasterMind's Main Menu"); 
      Console.WriteLine("     1: Play"); 
      Console.WriteLine("     2: Help"); 
      Console.WriteLine("     0: Exit"); 
     } 
     public void DrawHelp() 
     { 
      Console.Clear(); 
      Console.WriteLine("Rules Of MasterMind!"); 
      Console.WriteLine("Mastermind is a game about guessing a 4 digit code. The numbers can range from"); 
      Console.WriteLine("1-4 and any other numbers will be rejected. It will say in the CMD"); 
      Console.WriteLine("prompt whether or not you had any of the number correct or false."); 
      Console.WriteLine("Press any key to go back to the main menu."); 
      Console.ReadKey(); 
      Console.Clear(); 
      DrawMainMenu(); 
      Console.ReadLine(); 
     } 
     public void DrawExit() 
     { 
      Console.Clear(); 
      Console.WriteLine("You are about to exit the game"); 
      Console.WriteLine("Are you sure: Y/N"); 
      string userExit = Console.ReadKey().KeyChar.ToString(); 
      if (userExit == "Y") 
      { 
       Environment.Exit(0); 
      } 
      if (userExit == "N") 
      { 
       DrawMainMenu(); 
       Console.ReadLine(); 
      } 
     } 
    } 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var menu = new Menu(); 
      menu.DrawMainMenu(); 
      string userInput = Console.ReadKey().KeyChar.ToString(); 
      if (userInput == "1") 
      { 

      } 
      if (userInput == "2") 
      { 
       Console.Clear(); 
       menu.DrawHelp(); 
      } 
      if (userInput == "0") 
      { 
       menu.DrawExit(); 
       Console.ReadLine(); 
      } 
     } 
    } 

} 
+0

投票结束此问题,因为问题无法复制。 –

回答

0

这样的事情可以让你开始。

注意:您可以通过各种方式对结构和逻辑进行改进....我只给你一个例子,有Environment.Exit(0);它可以在... ...它可以通过在while循环有一个“标志”,这是设置,如果你选择“退出”,然后程序只是自然退出可以避免。

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

namespace ConsoleApplication3 
{ 
    using System; 

    namespace MasterMind 
    { 
     class Menu 
     { 
      public void DrawMainMenu() 
      { 
       Console.Clear(); 
       Console.ForegroundColor = ConsoleColor.Green; 
       Console.WriteLine("    MasterMind's Main Menu"); 
       Console.WriteLine("     1: Play"); 
       Console.WriteLine("     2: Help"); 
       Console.WriteLine("     0: Exit"); 
      } 
      public void DrawHelp() 
      { 
       Console.Clear(); 
       Console.WriteLine("Rules Of MasterMind!"); 
       Console.WriteLine("Mastermind is a game about guessing a 4 digit code. The numbers can range from"); 
       Console.WriteLine("1-4 and any other numbers will be rejected. It will say in the CMD"); 
       Console.WriteLine("prompt whether or not you had any of the number correct or false."); 
       Console.WriteLine("Press any key to go back to the main menu."); 
       Console.ReadKey(); 
      } 
      public void DrawExit() 
      { 
       Console.Clear(); 
       Console.WriteLine("You are about to exit the game"); 
       Console.WriteLine("Are you sure: Y/N"); 
       string userExit = Console.ReadKey().KeyChar.ToString(); 
       if (userExit.ToUpper() == "Y") 
       { 
        Environment.Exit(0); 
       } 
      } 
     } 
     class Program 
     { 
      static void Main(string[] args) 
      { 
       var menu = new Menu(); 

       while (true) 
       { 
        menu.DrawMainMenu(); 

        string userInput = Console.ReadKey().KeyChar.ToString(); 
        if (userInput == "1") 
        { 
         Console.Clear(); 
         Console.WriteLine("Playing..."); 
         Console.ReadKey(); 
        } 
        if (userInput == "2") 
        { 
         menu.DrawHelp(); 
        } 
        if (userInput == "0") 
        { 
         menu.DrawExit(); 
        } 
       } 
      } 
     } 
    } 
}