Venciity

[Jumpstart C#] Conditional statements - Valid date ?

Jan 24th, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _6.Valid_date_or_invalid_date
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.Write("day = ");
  14.             int day = Int32.Parse(Console.ReadLine());
  15.             Console.Write("month = ");
  16.             int month = Int32.Parse(Console.ReadLine());
  17.             Console.Write("year = ");
  18.             int year = Int32.Parse(Console.ReadLine());
  19.  
  20.             int lastDayOfTheMotnth =31 ;
  21.  
  22.             //if (month == 4 || month == 6 || month == 9 || month == 11)
  23.             //{
  24.             //    lastDayOfTheMotnth = 30;
  25.             //}
  26.             //else if (month == 2)
  27.             //{
  28.             //    bool isLeapYear = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
  29.             //    lastDayOfTheMotnth = isLeapYear ? 29 : 28;
  30.             //}   //работи по-бавно
  31.  
  32.             switch (month)
  33.             {
  34.                 case 4:
  35.                 case 6:
  36.                 case 9:
  37.                 case 11:
  38.                     lastDayOfTheMotnth = 30;
  39.                     break;
  40.                 case 2:
  41.                     bool isLeapYear = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
  42.                     lastDayOfTheMotnth = isLeapYear ? 29 : 28;
  43.                     break;
  44.             }  // switch работи по-бързо
  45.  
  46.             if (year >= 1  && day >= 1 &&
  47.                month >=1 && month <= 12 &&
  48.                day <= lastDayOfTheMotnth)
  49.                 Console.WriteLine("Valid date");
  50.             else
  51.                 Console.WriteLine("Invalid date");
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment