计算周几的程序(基姆拉尔森公式)
程序员文章站
2023-12-27 20:40:10
using System; namespace HELLO { class Program { int week; int y, m, d; static void Main(string[] args) { int y, m, d; Console.WriteLine("请输入年月日"); Con... ......
using system; namespace hello { class program { int week; int y, m, d; static void main(string[] args) { int y, m, d; console.writeline("请输入年月日"); console.writeline("现在请输入年份"); y = int.parse(console.readline()); console.writeline("现在请输入月份"); m = int.parse(console.readline()); console.writeline("现在请输入日期"); d = int.parse(console.readline()); console.writeline("您输入的年月日为" + y + m + d); program program = new program(); int week1 = program.weeks(d,m,y); if (week1 == 1) { console.writeline("周一"); } else if (week1 == 2) { console.writeline("周二"); } else if (week1 == 3) { console.writeline("周三"); } else if (week1 == 4) { console.writeline("周四"); } else if (week1 == 5) { console.writeline("周五"); } else if (week1 == 6) { console.writeline("周六"); } else if (week1 == 7) { console.writeline("周七"); } else { console.writeline("什么乱七八糟的"); } int week; //改良版的基姆拉尔森公式在计算1月和2月就完美了 if (m == 1 | m == 2) { m += 12; y--; } week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400 + 1) % 7; if (week == 1) { console.writeline("周一"); } else if (week == 2) { console.writeline("周二"); } else if (week == 3) { console.writeline("周三"); } else if (week == 4) { console.writeline("周四"); } else if (week == 5) { console.writeline("周五"); } else if (week == 6) { console.writeline("周六"); } else if (week == 7) { console.writeline("周七"); } else { console.writeline("什么乱七八糟的"); } } /// <summary> /// 用来输入年月日计算周几 /// </summary> /// <param name="d">日期</param> /// <param name="m">月份</param> /// <param name="y">年份</param> /// <returns></returns> int weeks(int d, int m, int y) { //单独的基姆拉尔森公式在计算1月和二月有缺陷 week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400 + 1) % 7; return ( week); } } }