2017-03-02 50 views
0

我有這個控制檯應用程序,這是非常基本的,你可以看到和IM試圖使它的地方時,它說爲什麼按下隨機字符時我的應用程序關閉?

Console.WriteLine(「你想回到主菜單?Y/N「);

並讓我說我不要按Y或N ..但是讓我們說F應用程序剛剛關閉..怎麼回事? 我應該用什麼方法來解決這個問題?

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

namespace Game 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      startMenu(); 


     } 

     public static void startMenu() 
     { 
      Console.WriteLine("======= Welcome To The Player Registry ======="); 
      Console.WriteLine("1. Name"); 
      Console.WriteLine("2. Age"); 
      Console.WriteLine("3. City"); 

      try 
      { 
       int value = Convert.ToInt32(Console.ReadLine()); 
       int answer = value; 
       switch (answer) 
       { 
        case 1: 
         Console.Clear(); 
         Console.WriteLine("======= Player Names======="); 
         Console.WriteLine("Jonsson, Adam"); 
         Console.WriteLine("Jetsson, Carl"); 
         Console.WriteLine("Jimmy, Golf"); 
         Console.WriteLine("Ali, Mohammed"); 
         Console.WriteLine(); 

         Console.WriteLine("Would you like to go back to the main menu? Y/N"); 
         string choice = Console.ReadLine(); 
         if (choice.Equals("Y", StringComparison.OrdinalIgnoreCase) || choice.Equals("y", StringComparison.OrdinalIgnoreCase)) 
         { 
          Console.Clear(); 
          startMeny(); 
         } 
         if (choice.Equals("N", StringComparison.OrdinalIgnoreCase) || choice.Equals("n", StringComparison.OrdinalIgnoreCase)) 
         { 
          Console.Clear(); 
          Console.WriteLine("Okay.. Bye!"); 
          Console.ReadKey(); 
         } 
         break; 

        case 2: 
         Console.WriteLine("Look it works!"); 
         break; 
        default: 
         Console.WriteLine("What?.."); 
         return; 

       } 
      } 
      catch 
      { 

      } 
     } 
    } 
} 
+6

遵循邏輯。如果「選擇」不是「Y」或「N」,那麼你跳出了開關......然後這就是程序的結束。 – Jonesopolis

+1

請勿以這種方式使用try-catch。請使用從控制檯讀取的行上的TryParse方法。 –

回答

0

因爲應用程序完成執行而碰到任何其他鍵時關閉。爲了強制應用程序等待,那麼您需要添加行Console.ReadLine();。你的情況,你可以更改代碼以這種

if (choice.Equals("Y", StringComparison.OrdinalIgnoreCase) || choice.Equals("y", StringComparison.OrdinalIgnoreCase)) 
{ 
    Console.Clear(); 
    startMeny(); 
} 
else if (choice.Equals("N", StringComparison.OrdinalIgnoreCase) || choice.Equals("n", StringComparison.OrdinalIgnoreCase)) 
{ 
    Console.Clear(); 
    Console.WriteLine("Okay.. Bye!"); 
    Console.ReadKey(); 
} 
else 
{ 
    Console.WriteLine("Wrong Key"); 
    Console.ReadLine(); 
} 
break; 

case 2: 
    Console.WriteLine("Look it works!"); 
    Console.ReadLine(); 
    break; 
default: 
    Console.WriteLine("What?.."); 
    Console.ReadLine(); 
    return; 
0

控制檯應用程序正在關閉,因爲它將只運行,直到它有沒有更多的事情。避免關閉程序的一個好方法是讓用戶選擇做其他事情。

這些都需要更多的代碼來適合自己的項目,但這裏僅僅是一個快速的想法:

你可以創建一些方法來幫助你:

public static string BackToMainMenu() 
    { 
     Console.WriteLine("Would you like to go back to the main menu? Y/N"); 
     return Console.ReadLine(); ///returns the user's choice in a string variable 
    } 

public static void CheckChoice(string choice) 
    { 
     if (choice == "y") 
     { 
      ///back at menu stuff goes here 
     } 

     if (choice == "n") 
     { 
      ///whatever you want to do on "n" 
     } 

     ///if user types anything other than y or n (could also use 'else') 
     if (choice != "n" && choice != "y") 
     { 
      Console.WriteLine("Incorrect Input"); 

      ///Ask user if they want to go back to menu (again) 
      ///check their choice 
      CheckChoice(BackToMainMenu()); 
     } 
    } 

取代你有什麼根據情況1與:

case 1: 
        Console.Clear(); 
        Console.WriteLine("======= Player Names======="); 
        Console.WriteLine("Jonsson, Adam"); 
        Console.WriteLine("Jetsson, Carl"); 
        Console.WriteLine("Jimmy, Golf"); 
        Console.WriteLine("Ali, Mohammed"); 
        Console.WriteLine(); 

        ///ask "back to menu?" and check choice 
        CheckChoice(BackToMainMenu()); 
相關問題