您好,登錄后才能下訂單哦!
static void Main(string[] args)
{
//請(qǐng)用戶輸入年份,再輸入月份,輸出該月的天數(shù).(結(jié)合之前如何判斷閏年來(lái)做);
//用到之前的知識(shí)點(diǎn)有if-else,switch-case,try-catch;
//這個(gè)程序只作為學(xué)習(xí)知識(shí)點(diǎn),比如多年和月多輸入不符合要求的就會(huì)報(bào)錯(cuò),這里不作深究;
Console.WriteLine("請(qǐng)輸入年份");
try
{ //把可能輸入不符合要求的try起來(lái)
int year = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請(qǐng)輸入月份");
try
{
int month = Convert.ToInt32(Console.ReadLine());
//判斷月份天數(shù)及是否是潤(rùn)年;
int day = 0; //聲明一個(gè)變量來(lái)存儲(chǔ)天數(shù);
if (month >= 1 && month <= 12)
{
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月有潤(rùn)年之分,排除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("輸入的年份不正確,請(qǐng)重新輸入");
}
}//這個(gè)括號(hào)跟catch跟月份的try配對(duì)
catch
{
Console.WriteLine("輸入的月份不正確,請(qǐng)重新輸入");
}
}//這個(gè)括號(hào)跟catch跟年份的try配對(duì)
catch
{
Console.WriteLine("輸入的年份不正確,請(qǐng)重新輸入");
}
Console.ReadKey();
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。