4、switch case 语句

Switch case 语句

输入一个年月日,判断是这一年的第几天?

            Console.WriteLine("请输入年:");

            int year = int.Parse(Console.ReadLine());

            Console.WriteLine("请输入月:");

            int month = int.Parse(Console.ReadLine());

            Console.WriteLine("请输入日:");

            int day = int.Parse(Console.ReadLine());

            int sum=0;

            if ((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0)))

            {

                switch (month)

                {

                    case 1:

                        sum =day;

                        break;

                    case 2:

                        sum =day + 31;

                        break;

                    case 3:

                        sum =day + 31 + 29;

                        break;

                    case 4:

                        sum =day + 31 + 29 + 31;

                        break;

                    case 5:

                        sum =day + 31 + 29 + 31 + 30;

                        break;

                    case 6:

                        sum =day + 31 + 29 + 31 + 30 + 31;

                        break;

                    case 7:

                        sum =day + 31 + 29 + 31 + 30 + 31 + 30;

                        break;

                    case 8:

                        sum =day + 31 + 29 + 31 + 30 + 31 + 30 + 31;

                        break;

                    case 9:

                        sum =day + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31;

                        break;

                    case 10:

                        sum =day + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30;

                        break;

                    case 11:

                        sum =day + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;

                        break;

                    case 12:

                        sum =day + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;

                        break;

                    default:

                        Console.WriteLine("输入错误");

                        break;

                }

            }

            else

            {

                switch (month)

                {

                    case 1:

                        sum =day;

                        break;

                    case 2:

                        sum =day + 31;

                        break;

                    case 3:

                        sum = day + 31 + 28;

                        break;

                    case 4:

                        sum =day + 31 + 28 + 31;

                        break;

                    case 5:

                        sum =day + 31 + 28 + 31 + 30;

                        break;

                    case 6:

                        sum =day + 31 + 28 + 31 + 30 + 31;

                        break;

                    case 7:

                        sum =day + 31 + 28 + 31 + 30 + 31 + 30;

                        break;

                    case 8:

                        sum =day + 31 + 28 + 31 + 30 + 31 + 30 + 31;

                        break;

                    case 9:

                        sum =day + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;

                        break;

                    case 10:

                        sum =day + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;

                        break;

                    case 11:

                        sum =day + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;

                        break;

                    case 12:

                        sum =day + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;

                        break;

                    default:

                        Console.WriteLine("输入错误");

                        break;

                }

            }

 

            Console.WriteLine("这是{0}年的第{1}天",year,sum);

转载于:https://www.cnblogs.com/XMH1217423419/p/4158309.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值