How to use switch case statement with user input value in C#

2 Answers

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Type a number: ");
            int n = int.Parse(Console.ReadLine());

            switch (n)
            {
                case 1:
                    Console.WriteLine("1");
                    break;
                case 2:
                    Console.WriteLine("2");
                    break;
                case 3:
                    Console.WriteLine("3");
                    break;
                case 4:
                    Console.WriteLine("4");
                    break;
                default:
                {
                    Console.WriteLine("default");
                    break;
                }
            }
        }
    }
}


/*
run:

Type a number: 2
2

*/

 



answered Dec 29, 2016 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Type a number: ");
            int n = int.Parse(Console.ReadLine());

            switch (n)
            {
                case 1:
                case 2:
                case 3:
                    Console.WriteLine("1 or 2 or 3");
                    break;
                case 4:
                    Console.WriteLine("4");
                    break;
                default:
                {
                    Console.WriteLine("default");
                    break;
                }
            }
        }
    }
}


/*
run:

Type a number: 2
1 or 2 or 3

*/

 



answered Dec 29, 2016 by avibootz

Related questions

1 answer 212 views
2 answers 258 views
3 answers 288 views
3 answers 236 views
2 answers 249 views
2 answers 228 views
...