C# 判断是否为闰年
关注我,带你了解C#的魅力
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 判断是否为闰年
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("输入一个年份,下面自动判断是否为闰年,如果输入1,默认退出");
//首先需要了解 4年一闰,100年不闰,4百年闰。
while (true) //此处了解下就好,这是用来无限循环的,省的运行一次就退出了
{
int year = Convert.ToInt32(Console.ReadLine()); //cinsole.readline()输出类型为string,年是数字类型,因此需要强制转换
if (year==1)
{
Console.Write("再按一次回车退出");
break;
}
else
{
if (year % 4 == 0)
{
if (year % 100 == 0) // 此处的%不是代表百分数,而是C#内的除余 除100看是否余0
{
if (year % 400 == 0)
{
Console.WriteLine("是闰年");
}
else
{
Console.WriteLine("不是闰年");
}
}
else
{
Console.WriteLine("是闰年");
}
}
else
{
Console.WriteLine("不是闰年");
}
}
}
Console.ReadLine();
}
}
}
运行结果: