標籤:一個 summary lap using ase class code nbsp src
1 using System; 2 using System.Globalization; 3 4 class Program 5 { 6 public static void Main(string[] args) 7 { 8 var year = GetInt(); 9 var month = GetMonth(); 10 var days = 0; 11 switch (month) 12 { 13 case 4: 14 case 6: 15 case 9: 16 case 11: 17 days = 30; 18 break; 19 case 2: 20 days = IsLeapYear(year) ? 29 : 28; 21 break; 22 default: 23 days = 31; 24 break; 25 } 26 print(days); 27 } //Main函數結束 28 29 /// <summary> 30 /// 獲得一個正確的月份 31 /// </summary> 32 /// <returns></returns> 33 private static int GetMonth() 34 { 35 var m = 0; 36 while (true) 37 { 38 m = GetInt(); 39 if (m > 12 || m < 0) 40 { 41 print("輸入的月份有誤,請重新輸入!"); 42 } 43 else 44 { 45 break; 46 } 47 } 48 return m; 49 } 50 51 /// <summary> 52 /// 判斷給定的年份是否是閏年 53 /// </summary> 54 /// <param name="year"></param> 55 /// <returns></returns> 56 public static bool IsLeapYear(int year) 57 { 58 return year % 400 == 0 || year % 4 == 0 && year % 100 != 0; 59 } 60 61 #region 工具方法 62 63 public static void print(string obj, params object[] arg) 64 { 65 Console.WriteLine(obj, arg); 66 } 67 68 public static void print(object obj) 69 { 70 Console.WriteLine(obj); 71 } 72 73 /// <summary> 74 /// 獲得一個int類型的值 75 /// </summary> 76 /// <returns></returns> 77 public static int GetInt() 78 { 79 int i; 80 while (true) 81 { 82 try 83 { 84 i = Convert.ToInt32(Console.ReadLine()); 85 break; 86 } 87 catch (FormatException e) 88 { 89 Console.WriteLine(e.Message); 90 } 91 } 92 return i; 93 } 94 95 public static string GetString() 96 { 97 return Console.ReadLine(); 98 } 99 100 public static double GetDouble()101 {102 double i;103 while (true)104 {105 try106 {107 i = Convert.ToDouble(Console.ReadLine());108 break;109 }110 // catch111 catch (FormatException e)112 {113 Console.WriteLine(e.Message);114 }115 }116 return i;117 }118 119 #endregion120 }View Code
c#基礎