C#使用IsLeapYear方法判断指定年份是否为闰年

目录

一、判断指定年是否为闰年的2个方法

1.使用IsLeapYear方法判断指定年份是否为闰年

2.使用自定义的算法计算指定年份是否为闰年

二、示例

1.方法1的实例

2.方法2的实例 


一、判断指定年是否为闰年的2个方法

1.使用IsLeapYear方法判断指定年份是否为闰年

        使用IsLeapYear方法判断指定年份是否为闰年,可以通过判断指定的年份是否为闰年的方式来计算天数,如果指定的年份是闰年则有366天,如果指定的年份不是闰年则有365天。

2.使用自定义的算法计算指定年份是否为闰年

        指定年份如果能被400整除就为闰年,或者指定年份可以整除4但不能整除100也为闰年。

二、示例

1.方法1的实例

//使用IsLeapYear方法判断指定年份是否为闰年
namespace _058
{
    public partial class Form1 : Form
    {
        private Button? button1;
        public Form1()
        {
            InitializeComponent();
            Load += Form1_Load;
        }
        private void Form1_Load(object? sender, EventArgs e)
        {
            // 
            // button1
            // 
            button1 = new Button
            {
                Location = new Point(98, 33),
                Name = "button1",
                Size = new Size(100, 23),
                TabIndex = 0,
                Text = "当前年天数",
                UseVisualStyleBackColor = true
            };
            button1.Click += Button1_Click;
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(284, 81);
            Controls.Add(button1);
            Name = "Form1";
            StartPosition = FormStartPosition.CenterScreen;
            Text = "获取当前年天数";          
        }

        private void Button1_Click(object? sender, EventArgs e)
        {
            if (DateTime.IsLeapYear(int.Parse(DateTime.Now.ToString("yyyy"))))//判断是否为闰年
            {
                MessageBox.Show("本年有366天", "提示!");//显示天数信息
            }
            else
            {
                MessageBox.Show("本年有365天", "提示!");//显示天数信息
            }
        }
    }
}

 

2.方法2的实例 

// 自定义方法计算何为闰年
namespace _058_1
{
    class Program
    {
        static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            Console.WriteLine("请输入要判断的年份:");
            int year = Convert.ToInt32(Console.ReadLine());

            if (IsLeapYear(year))
            {
                Console.WriteLine($"{year}年是闰年。");
            }
            else
            {
                Console.WriteLine($"{year}年不是闰年。");
            }
        }

        // 定义一个函数来判断指定年份是否为闰年
        public static bool IsLeapYear(int year)
        {
            return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
        }
    }
}
//运行结果:
/*
请输入要判断的年份:
2024
2024年是闰年。

 */

相关推荐

  1. 判断是否闰年?【C语言】

    2024-01-25 03:56:01       17 阅读
  2. C#:闰年判断

    2024-01-25 03:56:01       12 阅读
  3. js判断是否数字的方法

    2024-01-25 03:56:01       36 阅读
  4. C# 判定字符串是否数字的方法

    2024-01-25 03:56:01       10 阅读
  5. C语言判断一个数是否素数的三种方法(详细)

    2024-01-25 03:56:01       22 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-25 03:56:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-25 03:56:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-25 03:56:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-25 03:56:01       20 阅读

热门阅读

  1. Redis学习笔记

    2024-01-25 03:56:01       36 阅读
  2. pc端vue封装高德地图实现定位 PlaceSearch搜索

    2024-01-25 03:56:01       42 阅读
  3. linux和windows对比

    2024-01-25 03:56:01       29 阅读
  4. matlab查看源代码

    2024-01-25 03:56:01       37 阅读
  5. Package g++ is not configured yet.

    2024-01-25 03:56:01       34 阅读
  6. ABC337(A-C)

    2024-01-25 03:56:01       33 阅读
  7. 死锁面试题详解

    2024-01-25 03:56:01       33 阅读
  8. Koa框架

    2024-01-25 03:56:01       31 阅读
  9. JVM实战(29)——模拟栈内存溢出

    2024-01-25 03:56:01       29 阅读
  10. Docker离线安装

    2024-01-25 03:56:01       31 阅读