首页 > 代码库 > switch case 语句

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

switch case 语句