I got the following homework assignment -
"create a program which receives 3 inputs from the user - day month and year, and check if it is a valid date.
this has to be done with switch-case
so I know that I need to check if the month is between 1 and 12, and for every month to check if it has the right amount of days in it -
February - 28 days
months 4,6,9,11 - 30 days
months 3,5,7,8,10,12 - 31 days.
this is what I did -
static void Main(string[] args)
{
int month, day, year;
Console.Write("Enter day: ");
day = int.Parse(Console.ReadLine());
Console.Write("Enter month: ");
month = int.Parse(Console.ReadLine());
Console.Write("Enter year: ");
year = int.Parse(Console.ReadLine());
switch(month)
{
case 2:
if (day < 0 || day > 28)
{
Console.WriteLine("False date");
}
// #######################################################################################################
break;
case 4:
if (day < 0 || day > 30)
{
Console.WriteLine("False date");
}
break;
case 6:
if (day < 0 || day > 30)
{
Console.WriteLine("False date");
}
break;
case 9:
if (day < 0 || day > 30)
{
Console.WriteLine("False date");
}
break;
case 11:
if (day < 0 || day > 30)
{
Console.WriteLine("False date");
}
break;
// ##############################################################################################################
case 3:
if (day < 0 || day > 31)
{
Console.WriteLine("False date");
}
break;
case 5:
if (day < 0 || day > 31)
{
Console.WriteLine("False date");
}
break;
case 7:
if (day < 0 || day > 31)
{
Console.WriteLine("False date");
}
break;
case 8:
if (day < 0 || day > 31)
{
Console.WriteLine("False date");
}
break;
case 10:
if (day < 0 || day > 31)
{
Console.WriteLine("False date");
}
break;
case 12:
if (day < 0 || day > 31)
{
Console.WriteLine("False date");
}
break;
}
is there a way to make the code a bit shorter? it seems to me like I'm missing the whole point of the assignment, because I'm quite sure I did it in a primitive way...
I mean it's just checking for every month... the code seems to be really encumbered...
help would be appreciated.. thanks!

New Topic/Question
Reply
MultiQuote









|