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

输入年月,输出月份有几天(分别用了if——else和switch)

时间:2015-06-02 10:53:23      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

首先是switch做的

 

 class Program
    {
        static void Main(string[] args)
        {/*
          题目要求:请用户输入年份,输入月份,输出该月的天数。
          思路:一年中月份的情况有三种。
          第一种:1,3,5,7,8,10,12月是31天。
          第二种:4,6,9,11月是30天。
          第三种:要判断年份是否是闰年,闰年2月29天。
          平年2月28天。判断公式:year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)。          
          */
            Console.WriteLine("请输入任意年份");
            int year = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入任意月份");
            int month = Convert.ToInt32(Console.ReadLine());
            switch (month)//括号内跟的是要判断的表达式,结果必须是一个”值“(也就是一个确定的数)。
            {
                case 1://当switch中的表达式有多种相同结果时,可以把case依次列出,最后break。
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    Console.WriteLine("您输入的{0}年中的{1}月份有31天。", year, month);
                    break;
                case 2://case语句中嵌套了if——else的语句,来判断是否是闰年。
                    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))//判断闰年的表达式
                    {
                        Console.WriteLine("您输入的{0}年中的2月份有29天。", year);
                    }
                    else//不是闰年执行这里。
                    {
                        Console.WriteLine("您输入的{0}年中的2月份有28天。", year);
                    }
                    break;
                default ://case中剩余的情况就是月份是30天的。
                    Console.WriteLine("您输入的{0}年中的{1}月份有30天。", year);
                    break;
            }
            Console.ReadLine();
        }
    }
}

 

用if——else做的

 

   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 == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
            {
                Console.WriteLine("{0}年的{1}月份有31天。",year,month);
            }
            else if (month == 4 || month == 6 || month == 9 || month == 11)
            {
                Console.WriteLine("{0}年的{1}月份有30天。", year, month);
            }
            else
            {
                if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
                {
                    Console.WriteLine("{0}年的2月份有29天", year);
                }
                else
                {
                    Console.WriteLine("{0}年的2月份有28天",year);
                }
            }
            Console.ReadLine();
        }
    }
}

 

 

 

 

输入年月,输出月份有几天(分别用了if——else和switch)

标签:

原文地址:http://www.cnblogs.com/gchlcc/p/4545680.html

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