c#基礎

來源:互聯網
上載者:User

標籤:一個   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#基礎

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.