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

  • 相关阅读:
    SQL考点例题解析
    数据交换技术
    网络安全的攻击手段
    实词和虚词
    VBA代码
    宏代码
    常用模块
    常用模块
    模块和包
    模块介绍
  • 原文地址:https://www.cnblogs.com/XMH1217423419/p/4158309.html
Copyright © 2020-2023  润新知