Difference between revisions of "Switch"
esse quam videri
(→Example) |
(→Example) |
||
Line 12: | Line 12: | ||
choice = console.ReadLine(); | choice = console.ReadLine(); | ||
+ | choice.ToUpper(); // This will make the content of the string upper case so we don't have to check for both lower and upper case in our switch. | ||
switch(choice) | switch(choice) | ||
Line 29: | Line 30: | ||
case "W": | case "W": | ||
player.moveWest(); | player.moveWest(); | ||
+ | break; | ||
+ | |||
+ | default: | ||
+ | getPlayerInput(); | ||
break; | break; | ||
} | } |
Revision as of 18:16, 21 August 2019
Definition
Relevance
Example
Below is an example of a switch for an adventure game
console.WriteLine("You awake and find yourself at an intersection, which path will you take?");
console.WriteLine("Use N, S, E, or W for North, South, East, or West");
choice = console.ReadLine();
choice.ToUpper(); // This will make the content of the string upper case so we don't have to check for both lower and upper case in our switch.
switch(choice)
{
case "N":
player.moveNorth();
break;
case "S":
player.moveSouth();
break;
case "E":
player.moveEast();
break;
case "W":
player.moveWest();
break;
default:
getPlayerInput();
break;
}