← C# from Scratch Lesson 7 of 39 · 10 XP

Input

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

Console.ReadLine() reads a whole line (without the line break at the end). A number has to be extracted from it explicitly:

Console.Write("What is your name? ");
string name = Console.ReadLine();
Console.Write("How old are you? ");
int age = Convert.ToInt32(Console.ReadLine());   // or int.Parse(...)
Console.WriteLine($"{name}, in a year you'll be {age + 1}");

Several numbers on one line — split it:

string[] parts = Console.ReadLine().Split(' ');
int a = int.Parse(parts[0]);
int b = int.Parse(parts[1]);

If the input has run out, ReadLine() returns null. If the line isn't a number, int.Parse throws a FormatException (we'll cover error handling at the end of the course).

When tests check your program, input prompts aren't needed: print only what the task asks for.

Task. The first line is a name, the second is two whole numbers separated by a space. Print Hello, <name>! Sum: <a+b>.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.