2017-02-10 83 views
0
enum DaysInMonth 
    { 
     Jan = 31, Feb = 28, Mar = 31, Apr = 30, May = 31, Jun = 30, 
     Jul = 31, Aug = 31, Sep = 30, Oct = 31, Nov = 30, Dec = 31 
    }; 
DaysInMonth dm = DaysInMonth.Dec; 
     Console.WriteLine($"Number of days in {dm} is {dm:D}"); 

得到預期的結果獲得的天數在七月爲31,12月預計的天數是31不從枚舉

+4

可能的複製(http://stackoverflow.com/questions/8043027/non-unique-enum-values) – Steve

+0

枚舉應具有唯一值聲明。 – Pavvy

回答

4

它不是硬編碼在一個月的天數是個好主意。閏年呢?

正確的方法得到天在一個月:

DateTime.DaysInMonth(year, month) 
+0

它是一種完美的方式來獲得天的沒有。 – UJS

0

您正在試圖利用枚舉的。所以請嘗試關注代碼。 [非唯一枚舉值]的

enum DaysInMonth 
     { 
      Jan = 31, Feb = 28, Mar = 31, Apr = 30, May = 31, Jun = 30, 
      Jul = 31, Aug = 31, Sep = 30, Oct = 31, Nov = 30, Dec = 31 
     }; 



static void Main(string[] args) 
     { 
      DaysInMonth dm = DaysInMonth.Dec; 
      Console.WriteLine("Number of days in {0} is {1} : ", dm, (int)dm); 
      Console.ReadKey(); 
     }