繁体   English   中英

我如何使用月、日和年的输入(字符串)计算年龄?

[英]how can i calculate the age using the input (String) of the month,day and year?

        string Month, Year, Day;
        Console.Write("\nInsert birth month: ");
        Month = Console.ReadLine();

        if (Month.Equals("null"))
        {
            Console.Write("Insert birth day ");
            Console.Write("\nInsert birth year \n");

            
            Console.WriteLine("BIRTH MONTH:");
            Console.WriteLine("BIRTH DAY:");
            Console.WriteLine("BIRTH Year:");
            Console.WriteLine("BIRTHDAY:");


            Console.ReadKey();
        }
        else
        {
            Console.Write("Insert birth day: ");
            Day = Console.ReadLine();
            Console.Write("Insert birth year: ");
            Year = Console.ReadLine();

            
            Console.WriteLine("BIRTH MONTH: {0}", Month);
            Console.WriteLine("BIRTH DAY: {0}", Day);
            Console.WriteLine("BIRTH YEAR: {0}", Year);
            Console.WriteLine("BIRTHDAY:{0}/{1}/{2}", Month, Day, Year);
           Console.WriteLine("Age: {0}", DateTime.Now.Year - Convert.ToInt32(Year));
           Console.ReadKey();

           the only  variable that i can calculate is the year but when it comes to the month 
           and day it does not show correctly.

            The program runs like this:
            Insert birth month: 04
            Insert birth day:14
            Insert birth year:2000
         
            BIRTH MONTH:04
            BIRTH DAY:14
            BIRTH Year:2000
            BIRTHDAY:04/14/2000
            AGE: 22

The program runs like that because it does not considered the day and month to execute.

    The program should run like this:

            Insert birth month: 04
            Insert birth day:14
            Insert birth year:2000
         
            BIRTH MONTH:04
            BIRTH DAY:14
            BIRTH Year:2000
            BIRTHDAY:04/14/2000
            AGE: 21

如您所见,年龄是 21 岁,因为考虑的月份和日期是 4 月 14 日。 有谁可以帮助我吗?

我首先使用了 integer,但是“null”这个词不起作用,这就是为什么我使用字符串来解决这个问题。

如果我插入 null 工作,程序应该像这样运行:

插入出生月份:null 插入出生日期 插入出生年份

出生月份: 出生日: 出生年份: 生日: 年龄:

这是我关心的问题,如何考虑月、日和年这三个主要变量来计算年龄。 提前致谢

  • 出于闰年的考虑,准确计算年龄需要检查很多条件。请参阅此处的更多信息: How do I calculate someone's age based on a DateTime type birthday?

     using System; namespace ConsoleApp2 { internal class Program { static void Main(string[] args) { string FirstName, LastName = " "; char MiddleInitial = ' '; Console.Write("Insert first name: "); FirstName = Console.ReadLine(); if (FirstName.Equals("null")) { Console.Write("Insert middle initial "); Console.Write("\nInsert last name \n"); } else { Console.Write("Insert middle initial: "); MiddleInitial = Console.ReadLine()[0]; Console.Write("Insert last name: "); LastName = Console.ReadLine(); } string Month = "", Year = "", Day = ""; Console.Write("\nInsert birth month: "); Month = Console.ReadLine(); if (Month.Equals("null")) { Console.Write("Insert birth day "); Console.Write("\nInsert birth year \n"); Console.WriteLine("\nYour record"); Console.WriteLine("FIRST NAME:{0}", FirstName == "null"? "": FirstName); Console.WriteLine("MIDDLE INITIAL: {0}", MiddleInitial); Console.WriteLine("LASTNAME: {0}", LastName); Console.WriteLine("BIRTH MONTH:"); Console.WriteLine("BIRTH DAY:"); Console.WriteLine("BIRTH Year:"); Console.WriteLine("BIRTHDAY:"); Console.ReadKey(); } else { Console.Write("Insert birth day: "); Day = Console.ReadLine(); Console.Write("Insert birth year: "); Year = Console.ReadLine(); var DOB = string.Format($"{Day}/{Month } / {Year}"); var currentdate = DateTime.Now; var age = Age(DateTime.Parse(DOB), currentdate); Console.WriteLine("\nYour record"); Console.WriteLine("FIRST NAME:{0}", FirstName == "null"? "": FirstName); Console.WriteLine("MIDDLE INITIAL: {0}", MiddleInitial); Console.WriteLine("LASTNAME: {0}", LastName); Console.WriteLine("BIRTH MONTH: {0}", Month); Console.WriteLine("BIRTH DAY: {0}", Day); Console.WriteLine("BIRTH YEAR: {0}", DateTime.Now.Year - Convert.ToInt32(Year)); Console.WriteLine("BIRTHDAY:{0}/{1}/{2}", Month, Day, Year); Console.WriteLine("Age: {0} ", age); } } public static int Age(DateTime birthDate, DateTime laterDate) { int age; age = laterDate.Year - birthDate.Year; if (age > 0) { age -= Convert.ToInt32(laterDate.Date < birthDate.Date.AddYears(age)); } else { age = 0; } return age; } } }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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