2016-10-07 4 views
-2

私は年齢を計算するために、あなたが依頼する必要があるので、これは奇妙な質問ですが、年齢C#の日付の年のマイナス誕生年

namespace howOldAreYou 
{ 
    class Program 
    { 
     public static void Main(string[] args) 
     { 
      //variables 

      int birthYear; 
      string name; 
      System.DateTime moment = new System.DateTime(); 
      int year = moment.Year; 
      //int month = moment.Month; 
      //int day = moment.Day; 
      //int minute = moment.Minute; 
      //int second = moment.Second; 
      //int millisecond = moment.Millisecond; 

      //Find out person's name 
      Console.WriteLine("What is your name?"); 
      name = Console.ReadLine(); 

      //Find what year person was born 
      Console.WriteLine("What year were you born, " + name + "?"); 
      if (int.TryParse(Console.ReadLine(), out birthYear)) 
      { 
       Console.WriteLine(birthYear + "! Well you must be " + (year - birthYear)); 
      } 
      else 
      { 
       Console.WriteLine("Sorry that's an invalid year"); 
      } 

     } 
    } 
} 
+1

誕生年だけの年齢は計算できません。あなたの実際の質問は何ですか? – TyCobb

+0

あなたは1日も必要です。あなたは同じ生年があり、私とは異なる年齢になることができます。 – Dispersia

+0

あなたは実際に質問していません。 – hatchet

答えて

0

を計算するために、現在の年のマイナス誕生年をcalculateする方法を把握しようとしていますユーザーは昼と時間も提供する必要があります。しかし、私はあなたが "近似年齢"に行きたいと思っています。代わりの

System.DateTime.Now.Year 

:その場合は、単に次の使用デフォルトでは1元戻り現在の年を返すよう

new System.DateTime().Year; 

後者は、それに提供されるパラメータはありません。

+0

年齢の計算には日付が必要です。時間はありません。 –

+0

まあ、正確な時間を含める必要があります。本当に正確に(.NETでは)ダニを含める必要がありますが、それはそれほど遠すぎるかもしれません;-) –

+0

いいえ。あなたが年齢を求めているなら、あなたは「カレンダーで一年中完成」を求めています。時間が重要なのは、質問をしている人の時間帯や対象者の所在地に基づいて、どの日が「今日」であるかを判断することだけです。生年月日は、「何時間/分/秒/歳の人が人なのか」を知りたい場合にのみ重要です。その他のhttp://codeofmatt.com/2014/04/09/handling-birthdays-and-other-anniversaries/乾杯! :) –

関連する問題