Jump to content

Question on switches

- - - - -

  • Please log in to reply
7 replies to this topic

#1
live504

live504

    Newbie

  • Members
  • PipPip
  • 12 posts
            const string myName = "Brandon";
            const string coolName = "Bam";
            const string sillyName = "ploppy";
            Console.WriteLine("What is your name?");
            string usrName = Console.ReadLine();
            switch (usrName.ToLower())
            {
                case myName:
                case coolName:
                case sillyName:
                    Console.WriteLine("That's a very silly name.");
                    break;
            }
            Console.WriteLine("Hello {0}!", usrName);
            Console.ReadKey();
In my book it says that I can stack the case statements and if one of the conditions are met, it will execute the block of code under it. Well, if I type in brandon, it doesn't say "that's a very silly name". How come its doing that?

#2
Simonxz

Simonxz

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
You call toLower and Brandon has a capital letter in it

#3
d347hm4n

d347hm4n

    Newbie

  • Members
  • Pip
  • 8 posts
I would do the following:


            const string myName = "BRANDON";

            const string coolName = "BAM";

            const string sillyName = "PLOPPY";

            Console.WriteLine("What is your name?");

            string usrName = Console.ReadLine();

            switch (usrName.ToUpper())

            {

                case myName:

                case coolName:

                case sillyName:

                    Console.WriteLine("That's a very silly name.");

                    break;

            }

            Console.WriteLine("Hello {0}!", usrName);


So you can type into the console in any case, it will convert it to upper case and compare it against upper case. I hope that helps.

#4
Simonxz

Simonxz

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
He is doing the same but for lower case, he just forgot the uppercase B in Brandan and Bam

#5
TCristoforo

TCristoforo

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
It looks like you're missing a few 'break' statements, under the first 2 cases. Otherwise, the other posters are right, C# strings are case sensitive so a capital B is not the same as a lowercase b.

#6
Simonxz

Simonxz

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
He doesn't, in C# you can stack up cases and they work as "or".

It's the same as
if (myName == usrName.ToUpper() || coolName == usrName.ToUpper() || sillyName == usrName.ToUpper())

    Console.WriteLine("That's a very silly name.");



#7
TCristoforo

TCristoforo

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
Right, I get the idea of 'falling through', I just wasn't sure if that was part of the OP's intention. We're back to the string being case sensitive then :)

#8
GIT Solutions

GIT Solutions

    Newbie

  • Members
  • PipPip
  • 13 posts
According to me you first remove the toLower() function from the switch case and then execute the code .




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users