码迷,mamicode.com
首页 > 其他好文 > 详细

switch case 语句

时间:2014-12-11 20:40:00      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:style   ar   sp   on   bs   ad   ef   amp   as   

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);

switch case 语句

标签:style   ar   sp   on   bs   ad   ef   amp   as   

原文地址:http://www.cnblogs.com/XMH1217423419/p/4158309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!