2015-04-26 20 views
-3

我正在創建程序,用戶輸入一個數字,顯示與該月相關的月份(1 = 1月,2 = feburary,3 = 3月,....等) 但是,何時輸入數字並按下回車鍵,程序就會中斷。無意程序終止

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

namespace ConsoleApplication27 
{ 
    class Program 
    { 

     public static void Main() 
     { 
      int month = 0; 

      //Range 1-12 refers to Month 
      Console.WriteLine("Please enter Month in numerical increments (1-12)"); 
      month = int.Parse(Console.ReadLine()); 

      switch (month) 
      { 
       case 1: 
        Console.WriteLine("This is the First Month...January"); 
        break; 
       case 2: 
        Console.WriteLine("This is the Second Month...Februrary"); 
        break; 
       case 3: 
        Console.WriteLine("This is the Third Month...March"); 
        break; 
       case 4: 
        Console.WriteLine("This is the Fourth Month...April"); 
        break; 
       case 5: 
        Console.WriteLine("This is the Fifth Month...May"); 
        break; 
       case 6: 
        Console.WriteLine("This is the Sixth Month...June"); 
        break; 
       case 7: 
        Console.WriteLine("This is the Seventh Month...July"); 
        break; 
       case 8: 
        Console.WriteLine("This is the Eighth Month...August"); 
        break; 
       case 9: 
        Console.WriteLine("This is the Ninth Month...September"); 
        break; 
       case 10: 
        Console.WriteLine("This is the Tenth Month...October"); 
        break; 
       case 11: 
        Console.WriteLine("This is the Eleventh Month...November"); 
        break; 
       case 12: 
        Console.WriteLine("This is the Twelfth Month...December"); 
        break; 

       default: 
        Console.WriteLine("You have inputed an invalid Character"); 
        break; 
      } 


      //Attempt to looP code 

     } 

    } 
} 

不確定是什麼原因造成的終止

+0

在最後添加一個'Console.ReadLine()'? – SimpleVar

+0

這不是代碼的一部分,只是在輸入問題時出現意外。輸入並輸入仍過早結束程序。 – Bentus

+0

我假設你沒有得到任何異常。所以你最後需要添加'Console.ReadLine()' - 這將防止控制檯在調試時自動關閉。 – SimpleVar

回答

1

添加while循環代碼,並設置諸如「0」碼退出循環或終止程序。

public static void Main() 
    { 
     int month = 0; 
     bool exit = false; 
     while (!exit) 
     { 
      //Range 1-12 refers to Month 
      Console.WriteLine("Please enter Month in numerical increments (1-12)"); 
      month = int.Parse(Console.ReadLine()); 

      switch (month) 
      { 
       case 0: 
        exit = true; 
        break; 
       case 1: 
        Console.WriteLine("This is the First Month...January"); 
        break; 
       case 2: 
        Console.WriteLine("This is the Second Month...Februrary"); 
        break; 
       case 3: 
        Console.WriteLine("This is the Third Month...March"); 
        break; 
       case 4: 
        Console.WriteLine("This is the Fourth Month...April"); 
        break; 
       case 5: 
        Console.WriteLine("This is the Fifth Month...May"); 
        break; 
       case 6: 
        Console.WriteLine("This is the Sixth Month...June"); 
        break; 
       case 7: 
        Console.WriteLine("This is the Seventh Month...July"); 
        break; 
       case 8: 
        Console.WriteLine("This is the Eighth Month...August"); 
        break; 
       case 9: 
        Console.WriteLine("This is the Ninth Month...September"); 
        break; 
       case 10: 
        Console.WriteLine("This is the Tenth Month...October"); 
        break; 
       case 11: 
        Console.WriteLine("This is the Eleventh Month...November"); 
        break; 
       case 12: 
        Console.WriteLine("This is the Twelfth Month...December"); 
        break; 

       default: 
        Console.WriteLine("You have inputed an invalid Character"); 
        break; 
      } 
     } 
     //Attempt to looP code 

    }