C#基础 输入一个日期,输出该日是当年的第几天

使用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天。

效果如下:

C#中,你可以通过读取用户的输入来获取年月,然后通过计算来判断当前日期是该年的第几天。以下是使用for循环来实现这一功能的步骤: 1. 首先,使用`Console.ReadLine()`方法来接收用户输入的年、月、,并将它们转换为整数。 2. 接着,根据每个月的天数来累加天数,直到用户输入的月份之前。这里需要注意的是闰年2月份有29天,平年2月份有28天。 3. 然后,加上用户输入的月份的天数。 4. 最后,输出这个累计的天数,即为用户输入日期是该年的第几天。 下面是一个简单的示例代码: ```csharp using System; class Program { static void Main() { Console.WriteLine("请输入年份:"); int year = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入月份:"); int month = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入日期:"); int day = Convert.ToInt32(Console.ReadLine()); int totalDay = 0; // 每个月天数的数组,假设用户输入的年份已经是正确的,所以不考虑闰年2月是29天的情况 int[] daysOfMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // 如果是闰年,并且月份大于2,则天数加1 if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { daysOfMonth[1] = 29; } // 累加到用户输入月份的上一个月 for (int i = 0; i < month - 1; i++) { totalDay += daysOfMonth[i]; } // 加上用户输入的日期 totalDay += day; Console.WriteLine("这是该年的第 " + totalDay + " 天。"); } } ``` 这段代码没有处理输入错误的情况,实际应用中需要添加输入验证。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值