新聞中心
static void Main(string[] args)
{
//請用戶輸入年份,再輸入月份,輸出該月的天數(shù).(結(jié)合之前如何判斷閏年來做);
//用到之前的知識點(diǎn)有if-else,switch-case,try-catch;
//這個(gè)程序只作為學(xué)習(xí)知識點(diǎn),比如多年和月多輸入不符合要求的就會報(bào)錯(cuò),這里不作深究;
Console.WriteLine("請輸入年份");
try
{ //把可能輸入不符合要求的try起來
int year = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請輸入月份");
try
{
int month = Convert.ToInt32(Console.ReadLine());
//判斷月份天數(shù)及是否是潤年;
int day = 0; //聲明一個(gè)變量來存儲天數(shù);
if (month >= 1 && month <= 12)
{
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比沾益網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式沾益網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋沾益地區(qū)。費(fèi)用合理售后完善,10年實(shí)體公司更值得信賴。
switch (month)
{ //1,3,5,7,8,10,12 為31天;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
//2月有潤年之分,排除2月;
case 2:
if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)
{
day = 29;
}
else
{
day = 28;
}
break;
//剩下的2,4,6,9,11為30天;
default:
day = 30;
break;
}
//輸出該月的天數(shù);
Console.WriteLine("{0}年{1}月的實(shí)際天數(shù)是{2}", year, month, day);
}
else
{
Console.WriteLine("輸入的年份不正確,請重新輸入");
}
}//這個(gè)括號跟catch跟月份的try配對
catch
{
Console.WriteLine("輸入的月份不正確,請重新輸入");
}
}//這個(gè)括號跟catch跟年份的try配對
catch
{
Console.WriteLine("輸入的年份不正確,請重新輸入");
}
Console.ReadKey();
}
網(wǎng)站名稱:12.C#--結(jié)合之前知識點(diǎn)如何判斷潤年
標(biāo)題來源:http://fisionsoft.com.cn/article/ggpohg.html