C # if else, switch, bool

I am trying to code a simple text adventure. when I started coding the directions that the user can enter, I realized that the user can enter "northeast", "southwest", etc., so I thought I should do things for them.

My problem: case5 north-east does not start when I type north-east on the command line.

class Branches { static void main(string[] args) { Branch2(); } public static void Branch2() { Console.WriteLine(""); Console.WriteLine("(North ,East ,South ,West)"); 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"); //Console.ReadLine(); 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; else CaseId = 0; switch (CaseId) { case 1: Console.WriteLine(""); Console.WriteLine("you head north"); break; case 2: Console.WriteLine(""); Console.WriteLine("you head east"); break; case 3: Console.WriteLine(""); Console.WriteLine("you head south"); break; case 4: Console.WriteLine(""); Console.WriteLine("you head west"); break; case 5: Console.WriteLine(""); Console.WriteLine("you head north east"); break; default: Console.WriteLine("enter a valid direction"); break; } } } } 
+7
c # switch-statement if-statement boolean
source share
4 answers

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.

+4
source share

Since the input contains north, so first, if it is executing and not looking for else else, try to invert if and put case5 as the first if :)

  if (case5) CaseId = 5; else if (case2) CaseId = 2; else if (case3) CaseId = 3; else if (case4) CaseId = 4; else if (case1) CaseId = 1; 
+12
source share

"northeast"

 bool case1 = input.Contains("north"); //true bool case5 = input.Contains("north") && input.Contains("east"); //true if (case1) CaseId = 1; else //Done moving on 
+2
source share

It cannot work because you ask โ€œcontains the northโ€ as the first question? the answer is yes. You go to step 1. You will not go to step 5.

To go to step 5, you need to ask him first.

In any case, the code seems to have more problems. I'm not sure what you are trying to achieve, but it looks like you are complicating things for no reason.

I would do something similar to the following:

 List<string> eastWest = new List<string> {"east", "west"}; var up = new List<string> {"north", "south"}; var direction = Console.ReadLine(); var upDirection = up.Find(direction.Contains); var sideDirection = eastWest.Find(direction.Contains); var result = upDirection != null ? upDirection + " " + sideDirection:sideDirection; Console.WriteLine("You are headed" + result); 
+1
source share

All Articles