- 8

The usefull way of using switch statement :)

in this code i'm using switch statement to analyse user input :) you can use it in your way after understanding it.. if you have any problem regarding understanding you can reply to this post ;) dont forget to thumbs up :) static void Main(string[] args) { Console.Write("Enter Something for Checking : "); char choiceNumber; choiceNumber = Convert.ToChar(Console.ReadLine()); switch (choiceNumber) { case 'a': Console.WriteLine("something 1"); break; case 'b': Console.WriteLine("something 2"); break; case 'c': Console.WriteLine("something 3"); break; default: Console.WriteLine("You Failed :( "); break; } Console.ReadLine(); you can also use it with the int, bool or any other data types ;) try it

30th Aug 2016, 12:31 PM
Ammar Ahmad
Ammar Ahmad - avatar
1 Answer
+ 1
You lost one brace at the end. And you don't need "Console.ReadLine();" method in the end too. static void Main(string[] args) { Console.Write("Enter Something for Checking : "); char choiceNumber; choiceNumber = Convert.ToChar(Console.ReadLine()); switch (choiceNumber) { case 'a': Console.WriteLine("something 1"); break; case 'b': Console.WriteLine("something 2"); break; case 'c': Console.WriteLine("something 3"); break; default: Console.WriteLine("You Failed :( "); break; } } Also you need to catch exceptions if user input more than one char.
25th Nov 2016, 12:42 PM
Chill