Answer to Question #187780 in C# for CHANDRASENA REDDY

Question #187780

Create a Console application to test usage of Switch case construct. Accept some integer from user as command line argument and using a switch case construct, check if the value entered is 1, 2, 3, 4 or 5. Print some message in each case. If the value is other than the above values, then print error message.


1
Expert's answer
2021-05-01T08:12:01-0400
using System;


namespace SwitchCaseDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Input some integer: ");
            var input = Console.ReadLine();
            
            if (int.TryParse(input, out var number))
            {
                switch (number)
                {
                    case 1:
                        Console.WriteLine("You entered ONE");
                        break;
                    case 2:
                        Console.WriteLine("You entered TWO");
                        break;
                    case 3:
                        Console.WriteLine("You entered THREE");
                        break;
                    case 4:
                        Console.WriteLine("You entered FOUR");
                        break;
                    case 5:
                        Console.WriteLine("You entered FIVE");
                        break;
                    default:
                        Console.WriteLine("ERROR: The number entered was not expected!");
                        break;
                }
            }
            else
                Console.WriteLine("ERROR: The value entered is not an integer number!");
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS