2017-04-21 17 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(); 
        } 
       } 
      } 
     } 
    } 
} 
関連する問題