[C#]方法示例:判斷是否閏年

 

  1. using System; 
  2.  
  3. namespace Leap_Year 
  4.     internal class Program 
  5.     { 
  6.         private static void Main(string[] args) 
  7.         { 
  8.             Console.WriteLine("       閏年判斷         "); 
  9.             Console.WriteLine("************************"); 
  10.  
  11.             Console.WriteLine("請輸入一個年份:"); 
  12.             try 
  13.             { 
  14.                 int year = int.Parse(Console.ReadLine()); 
  15.  
  16.                 if (LeapYear(year)) 
  17.                 { 
  18.                     Console.WriteLine("您輸入的是一個閏年!"); 
  19.                 } 
  20.                 else 
  21.                 { 
  22.                     Console.WriteLine("您輸入的是一個非閏年!"); 
  23.                 } 
  24.             } 
  25.             catch 
  26.             { 
  27.                 Console.WriteLine("您輸入的不是一個正確的年份!"); 
  28.             } 
  29.             Console.ReadKey(); 
  30.         } 
  31.  
  32.         public static bool LeapYear(int year)   //定義一個方法來判斷是否閏年 
  33.         { 
  34.             if (year % 4 == 0 || year % 400 == 0 && year % 100 != 0) 
  35.             { 
  36.                 return true
  37.             } 
  38.             else 
  39.             { 
  40.                 return false
  41.             } 
  42.         } 
  43.     } 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章