1. 程式人生 > >判斷星期的方法(已知1900年1月1日是星期一)

判斷星期的方法(已知1900年1月1日是星期一)

            cc: Console.Write("輸入年份:");
            int year = int.Parse(Console.ReadLine());
            Console.Write("輸入月份:");
            int month = int.Parse(Console.ReadLine());
            Console.Write("輸入日期:");
            int day = int.Parse(Console.ReadLine());
            if (year >= 1900
&& month > 0 && month < 13 && day > 0 && day < 32) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { Console.WriteLine("{0}年是閏年", year); } else { Console.WriteLine("{0}年是平年"
, year); } int[] lyear = new int[] { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//閏年 int[] cyear = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//平年 int nowday = 0; if (year % 4 == 0 && year % 100 != 0 || year % 400
== 0) { for (int i = 0; i < month; i++) { if (i != month - 1) { nowday += lyear[i]; } else { nowday += day; } } } else { for (int i = 0; i < month; i++) { if (i != month - 1) { nowday += cyear[i]; } else { nowday += day; } } } int agoday = 0; for (int i = 1900; i<year; i++) { if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) { agoday += 366; } else { agoday += 365; } } int sumday = agoday + nowday; int week = sumday%7; switch (week) { case 1: Console.WriteLine("星期一"); break; case 2: Console.WriteLine("星期二"); break; case 3: Console.WriteLine("星期三"); break; case 4: Console.WriteLine("星期四"); break; case 5: Console.WriteLine("星期五"); break; case 6: Console.WriteLine("星期六"); break; case 0: Console.WriteLine("星期日"); break; default: break; } Console.ReadKey(); } else { Console.WriteLine("輸入有誤!"); goto cc; }