none
Beginner Question

    Question

  • Hello. I'm doing the online microsoft virtual academy to learn C#. I'd like to get a little practice using what I learned by creating a little console "story". I am unsure what is wrong here.

    Console.WriteLine("Hello eager Adventurer! Are you ready for your journey? Type Yes or No to continue");
                string a = Console.ReadLine();

                if (a == "Yes")
                {
                    Console.WriteLine("Very well! Make a choice!");
                    Console.WriteLine("(1) You journey to the land of mountains");
                    Console.WriteLine("(2) You journey through the shadowy forest");
                    
                    if (a == "1")
                    {
                        Console.WriteLine("You travel to the treachorous ands of Khangu. By speaking to a few locals you learn that an ancient treasure exists upon the summit of the greatest mountain");
                        
                    }
                    if (a == "2")
                    {
                        Console.WriteLine("You travel to the forest");
                    }
                }
                Console.ReadLine();  

    Yea, it's a little silly but a fun little project. The program gets the answer "Yes" and then moves on to the three Console.WriteLine lines. When I press 1 the console closes. I am unsure what I did wrong.

    Monday, January 13, 2014 12:42 AM

Answers

  • You need to get the user's input again after the second question.

                if (a == "Yes")
                {
                    Console.WriteLine("Very well! Make a choice!");
                    Console.WriteLine("(1) You journey to the land of mountains");
                    Console.WriteLine("(2) You journey through the shadowy forest");
                    a = Console.ReadLine(); // Add this line

    Monday, January 13, 2014 2:00 AM
  • Hi 7evenE1even,

    Ante's answer is nice. You also can use switch and case statements like that:

    if (a == "Yes")
                {
                    Console.WriteLine("Very well! Make a choice!");
                    Console.WriteLine("(1) You journey to the land of mountains");
                    Console.WriteLine("(2) You journey through the shadowy forest");
                    a = Console.ReadLine(); 
                    switch (a)
                    {
                        case "1":
                            Console.WriteLine("You travel to the treachorous ands of Khangu. By speaking to a few locals you learn that an ancient treasure exists upon the summit of the greatest mountain");
                            break;
                        case "2":
                            Console.WriteLine("You travel to the forest");
                            break;
                        default:
                            Console.WriteLine("Default case");
                            break;                    
                    }
               }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 13, 2014 2:16 AM
    Moderator

All replies

  • You need to get the user's input again after the second question.

                if (a == "Yes")
                {
                    Console.WriteLine("Very well! Make a choice!");
                    Console.WriteLine("(1) You journey to the land of mountains");
                    Console.WriteLine("(2) You journey through the shadowy forest");
                    a = Console.ReadLine(); // Add this line

    Monday, January 13, 2014 2:00 AM
  • Thank you. That works.

    Question. Why doesn't "Console.ReadLine();" work? Doesn't the line "a = Console.ReadLine();" mean Console.ReadLine() = Console.ReadLine() because I defined the string a before?

    Monday, January 13, 2014 2:15 AM
  • Hi 7evenE1even,

    Ante's answer is nice. You also can use switch and case statements like that:

    if (a == "Yes")
                {
                    Console.WriteLine("Very well! Make a choice!");
                    Console.WriteLine("(1) You journey to the land of mountains");
                    Console.WriteLine("(2) You journey through the shadowy forest");
                    a = Console.ReadLine(); 
                    switch (a)
                    {
                        case "1":
                            Console.WriteLine("You travel to the treachorous ands of Khangu. By speaking to a few locals you learn that an ancient treasure exists upon the summit of the greatest mountain");
                            break;
                        case "2":
                            Console.WriteLine("You travel to the forest");
                            break;
                        default:
                            Console.WriteLine("Default case");
                            break;                    
                    }
               }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 13, 2014 2:16 AM
    Moderator
  • When you have

    string a = Console.ReadLine();

    you are doing two things.  First this tells the compiler that there is a variable, called 'a', which is of type string.  Secondly, when the program runs, the ReadLine() method is called which will return a string - whatever the user typed.  The result of what the user typed is stored in the variable 'a'.

    Next, you check to see if 'a' is "Yes".  If it is you use WriteLine to display some instructions.  Now you check to see if 'a' is "1" but it cannot be.  You already know that it is "Yes".  The key is that 'a' has been set to the result of what is typed.  'a' is not set to Console.ReadLine(), so just testing 'a' will not get more input from the user.  That is why Ante Meridian has suggested that you add

    a = Console.ReadLine();

    The right hand side is executed which causes the system to wait for the user to type some text.  The text that the user types is the result of the right hand side.  That result is then saved in the variable 'a'.


    Paul Linton

    Monday, January 13, 2014 2:34 AM
  • Console.ReadLine() is a method that gets input from the keyboard and returns it. Think of it this way...

    Suppose you can't see the screen but I can. You say to me "Let me know what the user types". I wait until the user types something, then I report to you "The user just typed "Yes"". You do what you have to do based on that input, then you say, "Now tell me what the user types next", so I wait then say "2"". And so on.

    The key here is that you have to keep asking me each time. That is what "a = Console.ReadLine()" does. It asks for input, then stores that input in a string called "a". There is no way to say to the Console, "Give me the input whenever and wherever it occurs".

    The line "Console.Readline()" at the end asks for input, but doesn't store it (doesn't set "a" to be equal to it). This is the equivalent of saying "Let me know when the user has typed something, but I don't care what it is". Because it is right at the end, it is this line that will make the program pause and wait for the user to type something before exiting.
    Monday, January 13, 2014 2:39 AM
  • Thank you Paul and Ante. Those are very detailed explanations! Now I understand exactly what went wrong.

    Eason I will use the switch and case statements to fix up the code so it's more concise. Especially for this project it makes more sense to use that method.

    I have a final question. I may have missed this in the tutorials or it may have never been covered. I think if I posted the code it would be easier to understand my question.

                Console.WriteLine("Press 1");
                Console.ReadLine();
                if (Console.ReadLine() == "1")
                {
                    Console.WriteLine("BoogaBooga");
                    Console.ReadLine();
                }

    From what I can gather, the console is writing "Press 1". Then it asks for my input. I input "1". The if statement does not read my response. Why not? I set "Console.ReadLine() == "1"" and "1" is a string of letters. Hmm.         

    Monday, January 13, 2014 7:47 AM
  • Console.WriteLine("Press 1"); // Put some text onto the screen.
    Console.ReadLine(); // Wait for some user input, but don't store it.
    if (Console.ReadLine() == "1") // Wait for some user input again, and this time see if it's "1".
    {
        Console.WriteLine("BoogaBooga"); // Write some text to the scree.
        Console.ReadLine(); // Wait for user input again, and don't store it.
    }

    So, you are waiting for input twice in a row. You enter "1" at the first one, but then you get to the one in the "if" and the console waits for input again, and compares that input to "1", so the first input is lost.

    Remove the first Console.ReadLine() and it should work as you expect.
    • Proposed as answer by Kunal G Monday, January 13, 2014 9:10 AM
    Monday, January 13, 2014 9:02 AM
  • Ah. What a simple solution. Thank you for the definitions next to the code. They help.
    Monday, January 13, 2014 9:05 AM