代碼
namespace 作業(yè)2
{
class Program
{
static void Main(string[] args)
{
/**
* 請用戶輸年份,再輸入月份,輸出該月的天數(shù).
* */
Console.WriteLine("請輸入一個年份");
int year = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請輸入一個月份");
int month = Convert.ToInt32(Console.ReadLine());
if (month >= 1 && month <= 12)
{
int day = 0;//存儲天數(shù)
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月之間筋帖,程序退出!7沤印优俘!");
}
Console.ReadKey();
}
}
}
效果
IMG_0117.JPG