Advertisement
VyaraG

BeerTime

Nov 30th, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Threading;
  4.  
  5. //A beer time is after 1:00 PM and before 3:00 AM. Write a program that enters a time in format “hh:mm tt” (an hour in range [01...12], a minute in range [00…59] and AM / PM designator) and prints “beer time” or “non-beer time” according to the definition above or “invalid time” if the time cannot be parsed. Note that you may need to learn how to parse dates and times. Examples:
  6.  
  7. class BeerTime
  8. {
  9.     static void Main ()
  10. {
  11.     Console.Write("Enter what time it is now, but in format (hh:mm tt): ");
  12.         string input = Console.ReadLine();
  13.         DateTime time;
  14.         DateTime start = DateTime.Parse("1:00 PM");
  15.         DateTime end = DateTime.Parse("3:00 AM");
  16.         if (DateTime.TryParse(input, out time))
  17.         {
  18.             if ((time > start) || (time < end))
  19.             {
  20.                 Console.WriteLine("Beer time");
  21.             }
  22.             else
  23.             {
  24.                 Console.WriteLine("Non-beer time");
  25.             }
  26.         }
  27.         else
  28.         {
  29.             Console.WriteLine("Invalid time.");
  30.         }
  31.        
  32.    
  33.      }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement