我需要找到年龄最大和最小的学生。最终用户必须将生日输入到字符串中。
DateTime young = DateTime.MinValue;
DateTime old = DateTime.MinValue;
foreach (var d in students)
{
try
{
DateTime dt = Convert.ToDateTime(d.dob);
int result = DateTime.Compare(young, dt);
if (result < 0)
{
old = dt;
}
if (result > 0)
{
young = dt;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
}
Console.WriteLine("the youngest age is {0}",young);
Console.WriteLine("the oldest age is {0}", old);
逻辑有问题,我无法获得准确的输出。