Week 1: Lection 2

Oliver SeidlOliver Seidl
1 min read

Hello, and welcome, my fellow coders! Today, I learnt about user input and conversions. It's still very straightforward as it's basic theory from Coding 101. The only new thing I learnt was parsing. I confirmed my understanding of this by completing two tasks set by ChatGPT: the first prompts the user to input their name and age using int.Parse(), and the second prompts the user to input a number. It uses TryParse() to check if the number is valid, and prints whether it is even or odd. If not, it prints an error message.


Console.WriteLine("Zadejte své jméno:");
string jmeno = Console.ReadLine();

Console.WriteLine("Zadejte svůj věk:");
string vekInput = Console.ReadLine();
int vek = int.Parse(vekInput);

Console.WriteLine("Ahoj, " + jmeno + "! Za tři roky ti bude " + (vek + 3) + " let.");

Console.WriteLine("Zadejte číslo:");
string cisloInput = Console.ReadLine();

if (int.TryParse(cisloInput, out int cislo))
{
    if (cislo % 2 == 0)
    {
        Console.WriteLine("Zadané číslo " + cislo + " je sudé.");
    }
    else
    {
        Console.WriteLine("Zadané číslo " + cislo + " je liché.");
    }
}
else
{
    Console.WriteLine("Zadaná hodnota není platné číslo.");
}
0
Subscribe to my newsletter

Read articles from Oliver Seidl directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Oliver Seidl
Oliver Seidl