標籤:控制台
其中涉及一個演算法:
基姆拉爾森計算公式W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1) mod 7在公式中d表示日期中的日數,m表示月份數,y表示年數。注意:在公式中有個與其他公式不同的地方:把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10則換算成:2003-13-10來代入公式計算。代碼如下:
Console.WriteLine("**************************"); Console.WriteLine("輸入指定年月日查詢出星期幾"); Console.WriteLine("By:YYS"); Console.WriteLine("今天是{0}年{1}月{2}日 星期{3}", DateTime.Now.Year,DateTime.Now.Month, DateTime.Now.Day, (int)DateTime.Now.DayOfWeek); Console.WriteLine("**************************"); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("請輸入年:"); int y = System.Int32.Parse(Console.ReadLine()); Console.WriteLine("請輸入月"); int m = System.Int32.Parse(Console.ReadLine()); Console.WriteLine("請輸入日"); int d = System.Int32.Parse(Console.ReadLine()); int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400 + 1) % 7; Console.WriteLine("{0}年{1}月{2}日是星期{3}",y,m,d,week); Console.ReadKey();
c#控制台程式-“輸入指定年月日查詢出星期幾”