+ 1
User input
This part of the lesson is a bit confusing. Does anyone have an alternative explanation that I may better comprehend
2 Réponses
+ 4
Console is a class.
Inside that class is a method called ReadLine().
Methods will be further explained later in the course.
ReadLine() will prompt the user for input.
When the user inputs something readLine will then return you the users input as a String.
Return could be confusing, that's a bit further up in the course, but basically it will give you the String that the user enters.
String a = Console.ReadLine();
// a is whatever the user enters
Now, remember data-types?
String, int etc..
Well, if I enter a number it will be a String. I don't want to have to deal with a String if I want a number.
So, I can cast the String to an int.
int a = Convert.ToInt32(Console.ReadLine());
This will turn the String into an int, so we take the input as an int instead of a String.
+ 1
Console.WriteLine("What is your name:");
var yourName = Console.ReadLine();
That will prompt the user with a statement. In this case the statement is "What is your name:". The user will then type their response. Once they're done, that value will be stored in the variable "yourName".