Console.WriteLine("Hello welcome to the Next-Day calendar");
Console.WriteLine("Please write down the day");
int day = int.Parse(Console.ReadLine());
int day_next = day + 1;
Console.WriteLine("Please write down the month number");
int month = int.Parse(Console.ReadLine());
Console.WriteLine("Please write down the year");
int year = int.Parse(Console.ReadLine());
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
if (day_next <=31 && day > 0)
{
Console.WriteLine("The date is " + day_next + '/' + month + '/' + year);
}
if (day_next == 32)
{
Console.WriteLine("The day is " + 1 + '/' + (month+1) + '/' + year);
}
else
{
Console.WriteLine("Error!");
}
break;
case 2:
if (year % 4 == 0 && day_next <= 29 && day > 0 || year % 4 != 0 && day_next <= 28 && day > 0)
{
Console.WriteLine("The date is " + day_next + '/' + month + '/' + year);
}
if (year % 4 == 0 && day_next == 30 && day > 0 || year % 4 != 0 && day_next == 29 && day > 0)
{
Console.WriteLine("The date is " + 1 + '/' + (month+1) + '/' + year);
}
else
{
Console.WriteLine("Error!");
}
break;
case 4:
case 6:
case 9:
case 11:
if (day_next <= 30 && day > 0)
{
Console.WriteLine("The date is " + day_next + '/' + month + '/' + year);
}
if (day_next == 30)
{
Console.WriteLine("The date is " + 1 + '/' + (month + 1) + '/' + year);
}
else
{
Console.WriteLine("Error");
}
break;
case 12:
if (day_next <=31 && day > 0)
{
Console.WriteLine("The date is " + day_next + '/' + month + '/' + year);
}
if (day_next == 32)
{
Console.WriteLine("The day is " + 1 + '/' + 1 + '/' + (year + 1));
}
else
{
Console.WriteLine("Error!");
}
break;
default:
Console.WriteLine("Error!");
break;
}
如果我例如輸入:天= 22個月= 2年= 2015 它會寫「的日期是23/2/2015」,而下一行會寫錯誤!有人可以解釋這一點嗎?這段代碼似乎同時給了我if語句和else語句的結果,爲什麼?
我會建議你谷歌的一些簡單和免費的教程上的控制語句,特別關注'If Else'語句http://www.csharp-station.com/Tutorial/CSharp/Lesson03 – MethodMan
代碼執行邏輯。如果你打算繼續相同的邏輯,也許第二個if應該是「else if」。 –
設置一箇中斷點並逐步完成。看看哪些行正在執行,哪些不是。 – sab669