2015-04-26 133 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 

    }