【发布时间】:2021-04-30 22:43:10
【问题描述】:
这是我到目前为止的代码,我现在卡住了。
static void Main(string[] args)
{
int age = 0;
DateTime now = DateTime.Today;
Console.WriteLine("What is your age?");
age = Convert.ToInt32(Console.ReadLine());
}
【问题讨论】:
-
请详细说明是什么阻碍了您。你有什么问题?
-
如前所述。说明您遇到的具体问题。给出一个可重现的例子,以及期望的输出。这将使人们更容易为您提供帮助。
-
他们出生在哪一年总是模棱两可,这取决于他们今年是否过生日
-
@KeithNicholas 如果他们以毫秒为单位输入年龄...:D
-
我认为您正在寻找
int yearBorn = DateTime.Now.AddYears(-age).Year;(请注意,我们添加了他们年龄的负数,因为没有SubtractYears方法)。但正如@KeithNicholas 所说,如果他们还没有过生日,你必须在结果中加一个。或者你可以直接说Console.WriteLine($"You were born in {yearBorn} (or {yearBorn + 1} if you haven't had your birthday yet");