美文网首页
第六次作业第二题

第六次作业第二题

作者: 孤独是种安全感_b7bf | 来源:发表于2018-11-10 18:59 被阅读0次

    代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication2
    {
    class Program
    {
    static void Main(string[] args)
    {

            /**
             * 请用户输年份,再输入月份,输出该月的天数.
             * 
             * */
            //请用户输入
            Console.WriteLine("请输入年份");
            //接收用户输入,并转换成数字型字符
            int year = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入一个月份");
            int month = Convert.ToInt32(Console.ReadLine());
            if (month >= 1 && month <= 12)
            {
                int day = 0;//存储天数
                switch (month)
                {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12: day = 31;
                        break;
                    case 2:
                        if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
                        {
                            day = 29;
                        }
                        else
                        {
                            day = 28;
                        }
                        break;
                    default: day = 30;
                        break;
                }//swich
                Console.WriteLine("{0}年{1}月有{2}天", year, month, day);
            }//if
            else
            {
                Console.WriteLine("月份必须在1~12月之间,程序退出!!!");
                
            }
            Console.ReadKey();
        }
    }
    

    }

    效果

    IMG_20181107_151809.jpg

    相关文章

      网友评论

          本文标题:第六次作业第二题

          本文链接:https://www.haomeiwen.com/subject/rajvxqtx.html