Loading lessons...
Project 1: Personal Greeter
Project 1: Personal Greeter
Your first C# console app: ask for a name and age, then greet the user.
The goal
Read a name and an age, then print a friendly message that tells them how old they'll be next year.
What you practice
Console.WriteLineandConsole.ReadLineConvert.ToInt32casting- String interpolation with
quot;{x}" - A little arithmetic
Starter code
using System;
Console.WriteLine("What is your name?");
string name = Console.ReadLine();
Console.WriteLine("How old are you?");
int age = Convert.ToInt32(Console.ReadLine());
int nextYear = age + 1;
Console.WriteLine(quot;Hello {name}, next year you will be {nextYear}!");
Step-by-step
- Run the starter and answer the questions.
- Wrap the age conversion in a
try...catchthat handles a bad number. - Add a goodbye message at the end.
- Re-run and test with both valid and invalid ages.
Checklist
- [ ] The name is read with ReadLine()
- [ ] The age is converted with Convert.ToInt32
- [ ] The message uses interpolation
- [ ] Next year's age is correct
- [ ] A try/catch handles bad input
- [ ] The program runs without errors
TL;DR
ReadLine()always returns a string.- Convert it before doing math.
- Interpolation makes messages clean.