【发布时间】:2014-04-15 19:12:47
【问题描述】:
我的任务是在 Visual Studio 上用 C# 编写一个简单的程序,从控制台读取您的出生日期,打印您现在的年龄和十年后的年龄。
我找到了一个 Microsoft 教程并决定使用它,但它出错了。教程如下:http://channel9.msdn.com/Series/Twenty-C-Questions-Explained/14
我的任务没有完成,我看到以下错误:'The name age does not exist in the current context'。
namespace AgeAfterTenYears
{
class AgeAfterTenYears
{
static void Main()
{
Console.WriteLine("Enter birthdate - DD/MM/YYYY");
string birthdate = Console.ReadLine();
DateTime bd = Convert.ToDateTime(birthdate);
DateTime curDate = DateTime.Today;
if (bd > curDate)
{
Console.WriteLine("Birthdate must be equal to or before today");
}
else
{
int age = curDate.Year - bd.Year;
}
if (bd.Month > curDate.Month)
{
age--;
}
Console.WriteLine("You are: {0} years old", age);
}
}
}
我为这项任务找到了不同的解决方案,但我真的很想了解我的问题所在。可能这有点愚蠢,但我在第一节课后就这样做了......
任何帮助将不胜感激。
【问题讨论】:
标签: c# scope calculator