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?
7 replies to this topic
#1
Posted 12 March 2011 - 09:37 PM
|
|
|
#2
Posted 12 March 2011 - 10:37 PM
You call toLower and Brandon has a capital letter in it
#3
Posted 18 March 2011 - 02:53 AM
I would do the following:
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.
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
Posted 18 March 2011 - 08:10 PM
He is doing the same but for lower case, he just forgot the uppercase B in Brandan and Bam
#5
Posted 27 March 2011 - 06:03 AM
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
Posted 27 March 2011 - 06:34 AM
He doesn't, in C# you can stack up cases and they work as "or".
It's the same as
It's the same as
if (myName == usrName.ToUpper() || coolName == usrName.ToUpper() || sillyName == usrName.ToUpper())
Console.WriteLine("That's a very silly name.");
#7
Posted 27 March 2011 - 07:17 AM
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
Posted 09 April 2011 - 12:13 AM
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


Sign In
Create Account


Back to top









