简体   繁体   中英

Trying to make it in c# where the user enters their age and it tells them what year they were born in

Heres the code I have so far and I'm stuck at the moment.

    static void Main(string[] args)
    {
        int age = 0;
        DateTime now = DateTime.Today;

        Console.WriteLine("What is your age?");
        age = Convert.ToInt32(Console.ReadLine());

    }

An easy way to do this would to create a string variable that is a console.readline, convert the string to and int(age), and subtract age from the current year, the result of this equation will be the year they were born. However this is still not perfect as if you have not had your birthday yet it could be a year off.

String stringAge = Console.ReadLine();
int age = Convert.ToInt32(stringAge);
//year is 2021
int result = age-2021;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM