c# · level 1 · unit 2 · lesson 4
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
lets your program ask a question.
So far your programs only talk. To listen, they need : whatever the person types. Console.ReadLine() waits while they type an answer and press Enter.
It gives back what they typed as a string, so catch it in a box: string snack = Console.ReadLine();. Then use the box like any other.
Ask the question first with Console.Write, not WriteLine, so the answer is typed on the same line. Leave a space at the end of the question, inside the quotes.
Your program pauses at Console.ReadLine() until the person presses Enter.
guess first
Someone types crisps. What appears?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
using System;
Console.Write("Favourite snack? ");
string snack = Console.ReadLine();
Console.WriteLine("Yum, " + snack);
your turn
Your turn. Write the second question.
Write a question asking for a favourite food between the quotes on line 5. End it with a space. We will answer Sam and pancakes.
output
press run to see what happens
fix it
The answer went missing. Can you fix it?
output
press run to see what happens
- C# cannot find
name. Where is a box called name made? - Line 4 reads the answer, but no box catches it, so it is thrown away.
- Put
string name =in front ofConsole.ReadLine();on line 4, then press run.
nailed it
your program asked and listened. :D
Console.ReadLine()waits, then gives back what was typed.- The answer always comes back as a
string. - Catch the answer in a box to use it.
new words in your collection
- input
- Console.ReadLine
This lesson isn’t saved. A free account remembers every lesson you finish.