用C# 用户从键盘输入月份,使用switch语句输出该月份的天数
给你个控制台的代码吧,很简单的
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();
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;
}
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整除的为润年
广告 您可能关注的内容 |