永发信息网

用C# 用户从键盘输入月份,使用switch语句输出该月份的天数

答案:3  悬赏:80  手机版
解决时间 2021-02-15 09:53
  • 提问者网友:记得曾经
  • 2021-02-14 21:02
用C# 用户从键盘输入月份,使用switch语句输出该月份的天数
最佳答案
  • 五星知识达人网友:英雄的欲望
  • 2021-02-14 22:31
我这个保证你满意,你试试看呀!
Console.WriteLine("输入一个月份:");
int year = int.Parse(Console.ReadLine());
string month = Console.ReadLine();
switch (month)
{
case "1":
case "3":
case "5":
case "7":
case "8":
case "10":
case "12":
Console.WriteLine("31天");
break;
case "4":
case "6":
case "9":
case "11":
Console.WriteLine("是30天");
break;
case "2":
if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)
{
Console.WriteLine("29天");
}
else
{
Console.WriteLine("28天");
}
break;
default:
Console.WriteLine("错误");
break;
}
全部回答
  • 1楼网友:七十二街
  • 2021-02-15 01:05
int mouth;
Console.WriteLine("输入月份:");
mouth=Convert.ToInt16(Console.ReadLine());
if(mounth<=12&&mouth>0){
switch(mounth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
Console.WriteLine("{0}月有31天",mouth);
break;
case 2:
Console.WriteLine("2月闰年29天,平年28天");
break;
default:
Console.WriteLine("{0}月有30天",mouth);
}
}
else
{
Console.WriteLine("月份输入有误!");
}
Console.ReadLine();
行不?要是动态判断闰年平年的话,告诉你一下,年份能被4整除但不能被100整除或能被400整除的为润年
  • 2楼网友:过活
  • 2021-02-15 00:00

给你个控制台的代码吧,很简单的
Console.WriteLine("请输入年份:");
            string a = Console.ReadLine();
            Console.WriteLine("请输入月份数字:");
            string b=Console.ReadLine();
            string c = "";
            if (b == "2")
            {
                c = (Convert.ToInt32(a) % 4 == 0) ? (b + "闰年") : (b + "非闰年");
            }
            else
                c = b;
                switch (c)
                {
                    case "1":
                        Console.WriteLine("31天");
                        break;
                    case "2闰年":
                        Console.WriteLine("29天");
                        break;
                    case "2非闰年":
                        Console.WriteLine("28天");
                        break;
                    case "3":
                        Console.WriteLine("31天");
                        break;
                    case "4":
                        Console.WriteLine("30天");
                        break;
                    case "5":
                        Console.WriteLine("31天");
                        break;
                    case "6":
                        Console.WriteLine("30天");
                        break;
                    case "7":
                        Console.WriteLine("31天");
                        break;
                    case "8":
                        Console.WriteLine("31天");
                        break;
                    case "9":
                        Console.WriteLine("30天");
                        break;
                    case "10":
                        Console.WriteLine("31天");
                        break;
                    case "11":
                        Console.WriteLine("30天");
                        break;
                    case "12":
                        Console.WriteLine("31天");
                        break;
                }
            Console.ReadKey();
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯