反向年龄计算器

时间:2016-05-15 19:14:59

标签: c# visual-studio

我需要一些帮助,我希望你们可以帮助我。

我正在试图弄清楚如何计算一个人“ 100年”时的“日期”是什么

在我目前的代码中,我可以从“日期输入”计算“人员年龄

请帮助我弄清楚如何从“用户年龄输入”计算“日期”。

提前致谢

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Exer_13 { class Program { static void Main(string[] args) { /* ------------------------------------------ */ DateTime dt = DateTime.Now; String name = ""; DateTime birthDate; /* ------------------------------------------ */ Console.WriteLine("Current Date Is: " + dt); Console.WriteLine("\n\r"); Console.Write("Please Enter Your Name: "); name = Convert.ToString(Console.ReadLine()); Console.WriteLine("\n\r"); Console.Write("Enter your birtdate (MM/DD/YYYY): "); if (DateTime.TryParse(Console.ReadLine(), out birthDate)) { TimeSpan age = DateTime.Now - birthDate; Console.WriteLine("Your Name Is: " + name + " And Your age Is: {0} years and {1} days", (int)(age.Days / 365.25), age.Days % 365.25); Console.WriteLine("\n\r"); } else { Console.WriteLine("You have entered an invalid date." + Environment.NewLine); Console.WriteLine("\n\r"); } /* Code goes here */ Console.WriteLine("You will be 100 Years old At this Date: "); Console.ReadLine(); } } }

2 个答案:

答案 0 :(得分:2)

您只需要在出生日期加上年数。

var oneHundredth = birthDate.AddYears(100);

答案 1 :(得分:0)

更新您的代码,这将询问您希望获得日期后的年份数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exer_13
{
    class Program
    {
        static void Main(string[] args)
        {
        /* ------------------------------------------ */
       DateTime dt = DateTime.Now;
        String name = "";
        DateTime birthDate;
        DateTime futureDay;
        int age;

        /* ------------------------------------------ */
        Console.WriteLine("Current Date Is: " + dt);
        Console.WriteLine("\n\r");

        Console.Write("Please Enter Your Name: ");
        name = Console.ReadLine();
        Console.WriteLine("\n\r");

        Console.Write("Enter your birthday: ");
        if (DateTime.TryParse(Console.ReadLine(), out birthDate))
        {
            //Console.WriteLine("You will be 100 Years old At this Date: {0}", birthDate);
            Console.WriteLine("\n\r");
        }
        else
        {
            Console.WriteLine("You have entered an invalid year." + Environment.NewLine);
            Console.WriteLine("\n\r");
        }

        Console.Write("Enter age: ");
        if (int.TryParse(Console.ReadLine(), out age))
        {
            //TimeSpan age = DateTime.Now - birthDate;
            futureDay = birthDate.AddYears(age);
            Console.WriteLine("You will be 100 Years old At this Date: {0}", futureDay.ToString("dd MMM yyyy"));
            Console.WriteLine("\n\r");
        }
        else
        {
            Console.WriteLine("You have entered an invalid year." + Environment.NewLine);
            Console.WriteLine("\n\r");
        }


        /*
           Code goes here
        */


        Console.ReadKey();
        }
    }
}