Advertisement
Guest User

Pr10BeerTime

a guest
Sep 3rd, 2015
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 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. // A beer time is after 1:00 PM and before 3:00 AM. Write a program that enters a time in format
  8. // “hh:mm tt” (an hour in range [01...12], a minute in range [00…59] and AM / PM designator) and
  9. // prints “beer time” or “non-beer time” according to the definition above or “invalid time” if
  10. // the time cannot be parsed. Note that you may need to learn how to parse dates and times.
  11.  
  12. class Pr10BeerTime
  13. {
  14.     static void Main(string[] args)
  15.     {
  16.         Console.Write("Enter how many tests you would like to run: ");
  17.         int tests = int.Parse(Console.ReadLine());
  18.         for (int test = 0; test < tests; test++)
  19.         {
  20.             string time = Console.ReadLine();
  21.             int timeLenght = time.Length;
  22.             string minutes = time.Substring(timeLenght - 5, 2);
  23.             int minutesInt = int.Parse(minutes);
  24.             string designator = time.Substring(timeLenght - 2, 2);
  25.             string hours;
  26.             if (timeLenght == 7)
  27.             {
  28.                 hours = time.Substring(0, 1);
  29.             }
  30.             else
  31.             {
  32.                 hours = time.Substring(0, 2);
  33.             }
  34.             int hoursInt = int.Parse(hours);
  35.             int totalTime = hoursInt * 60 + minutesInt;
  36.             if (totalTime >= 60 && designator.Equals("PM"))
  37.             {
  38.                 Console.WriteLine("beer time");
  39.                 continue;
  40.             }
  41.             else if (totalTime < 180 && designator.Equals("AM"))
  42.             {
  43.                 Console.WriteLine("beer time");
  44.                 continue;
  45.             }
  46.             else
  47.             {
  48.                 Console.WriteLine("non-beer time");
  49.             }
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement