名称上下文在当前上下文中不存在 [英] Name age does not exist in the current context

查看:126
本文介绍了名称上下文在当前上下文中不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个任务,要在Visual Studio中用C#编写一个简单的程序,该程序可以从控制台读取您的生日,并打印出现在的年龄以及十年后的年龄。

I have an assignment to write a simple program in C# on Visual Studio that reads your birth date from the console, prints back your age now and your age after ten years.

我找到了Microsoft教程并决定使用它,但是它出错了。这是本教程: http://channel9.msdn.com/Series/Twenty -C-Questions-Explained / 14

I've found a Microsoft tutorial and decided to use it but it went wrong. Here's the tutorial: http://channel9.msdn.com/Series/Twenty-C-Questions-Explained/14

我没有完成任务,看到以下错误:'The名称年龄在当前上下文中不存在。

I'm not done with the task and I see the following errors: '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);
        }
    }
}

我找到了不同的解决方案完成任务,但我真的很想了解我的问题所在。也许这很愚蠢,但是我在第一堂课之后就开始这样做了。

I've found different solutions for the task but I'd really like to understand what's wrong with mine. Probably it's something stupid but I'm doing this after the first lesson so...

任何帮助将不胜感激。

推荐答案

    using System;

    namespace AgeAfterTenYears
    {
    class AgeAfterTenYears
    {
    static void Main()
    {
        var age = 0;
        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
        {
            age = curDate.Year - bd.Year;
        }
        if (bd.Month > curDate.Month)
        {
           age = --set your age here//curDate.Year - bd.Year
        }
        Console.WriteLine("You are: {0} years old", age);
    }

}

这篇关于名称上下文在当前上下文中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆