如何创建一个年龄计算器,以小时,天和年为单位说明年龄?

时间:2016-12-24 14:38:05

标签: c#

另一个新手问题。我正在尝试创建一个年龄计算器,它使用户年龄,然后从当前日期减去它并将其显示给用户。 我有基本的想法。 这是我的示例代码:

sqoop import-all-tables --connect your-database --username user --password pwd --m 1

2 个答案:

答案 0 :(得分:0)

试试这个:

static void Main(string[] args)
        {

            DateTime Current = DateTime.Now;
            Console.WriteLine("Please enter your birth date: ");
            string myBirthDate = Console.ReadLine();
            var birthDate = DateTime.Parse(myBirthDate);

           TimeSpan myAge = Current - birthDate;
           Console.WriteLine($"Hours: {myAge.TotalHours}");
           Console.WriteLine($"Days: {myAge.TotalDays}");
           Console.WriteLine($"Years: {Current.Year - birthDate.Year}");

           Console.WriteLine(myAge);
        }

答案 1 :(得分:-1)

#include<iostream>
#include<ctime>
#include<cmath>

using namespace std;

void getAge(int,int,int);
int main(){
    int mm,dd,yr;
    cout<<"                                     WELCOME TO AGE CALCULATOR\n";
    cout<<"                                     _________________________"<<endl;
    cout<<"ENTER YOUR DOB IN DD/MM/YR\n";
    cin>>dd>>mm>>yr;
    getAge(dd,mm,yr);
    
    return 0;
}

void getAge(int dd,int mm,int yr){

    yr=yr-1900;
    mm=mm-1;

    time_t t=time(NULL);
    struct tm* timeStruct = localtime(&t);
    cout<<abs(dd-(timeStruct->tm_mday))<<"-"<<abs(mm-(timeStruct->tm_mon))<<yr-(timeStruct->tm_year)<<endl;
  }