使用switch判断语句来实现,先实现输入日期和接受日期
定义 m2变量来接受二月的天数,因为年份要判断平闰年
判断条件
year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)
int m2 = 28;
Console.WriteLine("请输入年份");
int year = int.Parse(Console.ReadLine());
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
{
m2 = 29;
}
Console.WriteLine("请输入月份");
int mon = int.Parse(Console.ReadLine());
Console.WriteLine("请输入日期");
int days = int.Parse(Console.ReadLine());
接下来使用switch语句来判断,输入一月,那天数就是输入的天数,二月就是一月加上输入的天数,三月就是加上一月二月加上输入的天数,以此类推。
效果如下
int num =0;
switch (mon)
{
case 12:
num = 31 + m2 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 +30+ days;
Console.WriteLine(year+"年的第"+num+"天");
break;
case 11:
num = 31 + m2 + 31 + 30 + 31 + 30 + 31 + 31 + 30 +31+ days;
Console.WriteLine(year + "年的第" + num + "天");break;
case 10:
num = 31 + m2 + 31 + 30 + 31 + 30 + 31 + 31 +30+ days;
Console.WriteLine(year + "年的第" + num + "天");break;
case 9:
num = 31 + m2 + 31 + 30 + 31 + 30 + 31 +31+ days;
Console.WriteLine(year + "年的第" + num + "天");
break;
case 8:
num = 31 + m2 + 31 + 30 + 31 + 30 +31+ days;
Console.WriteLine(year + "年的第" + num + "天");
break;
case 7:
num = 31 + m2 + 31 + 30 + 31 + 30+ days;
Console.WriteLine(year + "年的第" + num + "天");
break;
case 6:
num = 31 + m2 + 31 + 30 +31+ days;
Console.WriteLine(year + "年的第" + num + "天");
break;
case 5:
num = 31 + m2 + 31 +30 +days;
Console.WriteLine(year + "年的第" + num + "天");
break;
case 4:
num = 31 + m2 +31+ days;
Console.WriteLine(year + "年的第" + num + "天");
break;
case 3:
num = 31 +m2+ days;
Console.WriteLine(year + "年的第" + num + "天");
break;
case 2:
num = 31+days;
Console.WriteLine(year + "年的第" + num + "天");
break;
case 1:
num += days;
Console.WriteLine(year + "年的第" + num + "天");
break;
default:
Console.WriteLine("输入错误");
break;
}
2000年为润年,共有366天,2022年为平年,共有365天。
效果如下: