Advertisement
A4L

Chapter - 14 - Enumeration

A4L
Dec 7th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.56 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. /*Try It Out!
  7. Months of the Year. Using the DaysOfWeek enumeration as an example, create an enumeration to
  8. represent the months of the year. Assign them the values 1 through 12. Write a program to ask the
  9. user for a number between 1 and 12. Check to be sure that they gave you a value in the right range
  10. and use an explicit cast to convert the number to your month enumeration. Then, using a switch
  11. statement or if statement to print out the full name of the month they entered.*/
  12. namespace Chapter___14___Enumeration
  13. {
  14.     enum MonthsOfYear {January=1, Feburay=2, March=3, April=4, May=5, June=6, Jully=7, August=8, September=9, October=10, November=11, December=12};
  15.     class Program
  16.     {
  17.         static void Main(string[] args)
  18.         {
  19.             Console.Write("\nPlease Enter a number between 1 and 12 : ");
  20.             string userInput = Console.ReadLine();
  21.  
  22.             if (int.TryParse(userInput, out int input))
  23.             {
  24.                 if (input >= 1 && input <= 12)
  25.                 {
  26.                     MonthsOfYear month = (MonthsOfYear)input;
  27.                     Console.Write($"You have entered the value : {input}");
  28.                     Console.Write($". This is the month : {month}\n");
  29.                 }
  30.                 else { Console.WriteLine("Please, only enter a value of 1-12"); }
  31.             }
  32.             else { Console.WriteLine("Please, only enter a number"); }
  33.  
  34.             Console.ReadKey();
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement