While this has been answered, I will make one remark:
Your logical values โโare not needed and help hide the problem.
For example:
string input = Console.ReadLine(); bool case1 = input.Contains("north"); bool case2 = input.Contains("east"); bool case3 = input.Contains("south"); bool case4 = input.Contains("west"); bool case5 = input.Contains("north") && input.Contains("east"); int CaseId; if (case1) CaseId = 1; else if (case2) CaseId = 2; else if (case3) CaseId = 3; else if (case4) CaseId = 4; else if (case5) CaseId = 5;
matches this:
string input = Console.ReadLine(); int CaseId; if (input.Contains("north")) CaseId = 1; else if (input.Contains("east")) CaseId = 2; else if (input.Contains("south")) CaseId = 3; else if (input.Contains("west")) CaseId = 4; else if (input.Contains("north") && input.Contains("east")) CaseId = 5;
Which should make it obvious enough that the expression "input.Contains (" north ") && input.Contains (" east ") 'will never be run because it requires input. Contains (" north ") to be false (thanks "else"). Debugging and switching helps a lot here.
So, as a programmer, loyal to be concise with your code - never write more than you need. In your code, you have a bunch of logical elements, a bunch of if and switch - all effectively do the same job.
I would write it like this:
string input = Console.ReadLine(); if (input.Contains("north") && input.Contains("east")) Console.WriteLine("\nyou head north east"); else if (input.Contains("north")) Console.WriteLine("\nyou head north"); else if (input.Contains("east")) Console.WriteLine("\nyou head east"); else if (input.Contains("south")) Console.WriteLine("\nyou head south"); else if (input.Contains("west")) Console.WriteLine("\nyou head west"); else Console.WriteLine("enter a valid direction");
This is the same in readability and comprehensibility, but includes a third line and therefore a third chance of error.
NPSF3000
source share