1

I'm new to programming
i started to learn C# language

        Console.WriteLine("yes or no ?");
        string answer = Console.ReadLine();
        switch (answer)
        { 
            case "yes":
                // some code 
                break;
            case "no":
                //some code 
                break;
            default:
                //some code 
                break;

my question is if the user inserted his answer "Yes", "YES" ,"yEs" or whatever the way he wrote "yes" the program will execute the default code because its not exactly matching to the lowercase "yes" i wrote in the switch case .. is there an advanced way to let the program detect that "Yes" with the uppercase letter is the the same answer to "yes" and then execute the block code of case "yes".

my 2nd question i picked C# to be my first language because Unity3d game engine support this language so i decided to learn the language at first then use it inside the unity3d is that a right decision

im sorry if my question seems stupid but as i said im newbie

Kamal Hesham
  • 11
  • 1
  • 1
  • 3

3 Answers3

5

Convert the user's input to lowercase using String.ToLower() before comparing it against your (lowercase) choices. You should also consider removing leading/trailing whitespace from the string as well, using String.Trim():

    switch (answer.Trim().ToLower())
    { 
        case "yes":
            // some code 
            break;
        case "no":
            //some code 
            break;
        default:
            //some code 
            break;
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
0

You have to convert his answer to upper or lower case so it'll be recognized regardless.

switch (answer.ToUpper())
{ 
    case "YES":
        // some code 
        break;
    case "NO":
        //some code 
        break;
    default:
        //some code 
        break;

OR

switch (answer.ToLower())
{ 
    case "yes":
        // some code 
        break;
    case "no":
        //some code 
        break;
    default:
        //some code 
        break;

And as for picking C# as your first langauge, it's completely fine if you're looking to make games in Unity. If you wish to try GameMaker, learning Java will do good for you.

I don't agree with all the people saying that Python should be your first language. It all depends on what you wanna code and for your cause, C# is very good.

Bizhan
  • 16,157
  • 9
  • 63
  • 101
T. K.
  • 112
  • 1
  • 8
0

You can use the following:

switch(answer.ToLower())

This will convert all the characters to lowercase.

Regarding the Unity 3D, I think C# is really very flexible and extensive language and it will let you do many things. It keeps getting better with every release and you have vast collections of namespaces and functions to help you through it.

For learning things related to Unity 3D Microsoft also provides video tutorials for the beginner to high level at https://mva.microsoft.com/training-topics/game-development#!jobf=Developer&lang=1033

At this link you can find bunch of video series by Microsoft which will get you started.

Abhishek Maurya
  • 321
  • 1
  • 3
  • 13