我被要求编写一个程序来验证用户输入的日期。日期必须输入为三个整数变量,分别代表日、月和年。输出必须说明日期是否有效。
听起来很简单……我需要做的就是让用户输入日期、月份和年份。然后调用它并将其显示为日期。但是我可能希望用户先输入月份,因为这样我就可以计算出该月有多少天。
我认为我需要做的是从一个数组或 3 开始,我需要让程序记住每个月有多少天,所以如果月份输入为 2 月,它不会接受任何大于 29 的输入。
在我开始晚上看 Visual Studio 之前,我是否认为任何地方都是正确的?或者我可以采取另一个角度吗?
好的,所以我一直在努力,这就是我所拥有的。
int monthentered = 0;
int dayentered = 0;
int year = 0000;
int [] month = new int [12];
int [] day = new int [31];
bool leap = false;
for (int x = 0; x <= 11; x++)
{
month[x] = x+1;
}
for (int x = 0; x <= 30; x++)
{
day[x] = x+1;
}
Console.WriteLine("Please enter a year...");
year = (Convert.ToInt16(Console.ReadLine()));
Console.WriteLine("{0}", year);
Console.WriteLine("Please enter a month...");
monthentered = (Convert.ToInt16(Console.ReadLine()));
Console.WriteLine("Please enter a day...");
dayentered = (Convert.ToInt16(Console.ReadLine()));
while (monthentered == 01 || 03 || 05 || 07 || 08 || 10 || 12)
{
while (dayentered == 31)
{
Console.WriteLine("There are only 30 days in this month please re-enter your day...");
dayentered = (Convert.ToInt16(Console.ReadLine()));
}
}
while (monthentered == 02)
{
while (dayentered > 28)
{
Console.WriteLine("There are only 28 days in this month please re-enter your day...");
dayentered = (Convert.ToInt16(Console.ReadLine()));
}
}
Console.WriteLine("{0}/{1}/{2}", dayentered, monthentered, year);
Console.ReadKey();
while monthenter = bit 的问题。有人可以为我做错什么提供帮助吗?