zoukankan      html  css  js  c++  java
  • 计算周几的程序(基姆拉尔森公式)

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

     

  • 相关阅读:
    java的instanceof简单使用
    PHP中return,exit,die的区别
    C#调用Dll文件中方法的简单应用
    C#的托管和非托管的简单理解
    对象造型(引用类型转换)
    面向对象3大特性的简单理解
    css3自定义滚动条背景透明
    console.time方法与console.timeEnd方法
    IE8 input X 去掉文本框的叉叉和密码输入框的眼睛图标
    JavaSript模块规范
  • 原文地址:https://www.cnblogs.com/BruceKing/p/11772692.html
Copyright © 2011-2022 走看看