Switch C#
Switch pozwala wykonać instrukcje w oparciu o wartość zmiennej.
int sw = 1;
switch (sw)
{
case 0:
Console.WriteLine("Case 0");
break;
case 1:
Console.WriteLine("Case 1");
break;
case 2:
Console.WriteLine("Case 2");
break;
default:
Console.WriteLine("default");
break;
}
Program z prostym menu do wyświetlania żartu lub cytatu
Console.WriteLine("1 - Joke");
Console.WriteLine("2 - Quote");
string menuOption = Console.ReadLine();
switch (menuOption)
{
case "1":
Console.WriteLine("What did one ocean say to the other ocean?");
Console.ReadKey();
Console.WriteLine("Nothing, it just waved. ");
break;
case "2":
Console.WriteLine("You only fail, if you stop trying.");
break;
default:
Console.WriteLine("No option selected ");
break;
}