计算周几的程序(基姆拉尔森公式)

Posted bruceking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计算周几的程序(基姆拉尔森公式)相关的知识,希望对你有一定的参考价值。

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);
        }
    }

}

技术图片

 

 技术图片

 

以上是关于计算周几的程序(基姆拉尔森公式)的主要内容,如果未能解决你的问题,请参考以下文章

[ C ]根据年月日判断周几(基姆拉尔森计算公式)

计算星期几基姆拉尔森公式

计算星期几基姆拉尔森公式

基姆拉尔森,输入年月日输出周几

用基姆拉尔森计算公式做一个课表

蔡基姆拉尔森计算公式