Name age does not exist in the current context

一笑奈何 提交于 2020-01-06 08:28:14

问题


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.

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

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...

Any help would be appreciated.


回答1:


    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);
    }

}



回答2:


You are declaring age in the else statement:

else
{
    int age = curDate.Year - bd.Year;
}

This means that the code outside that else statement cannot "see" the variable age. It is out of scope.

You should move the variable declaration above the first if statement.




回答3:


Probably you should modify your else part as follows:

else
{
    int age = curDate.Year - bd.Year;
    if (bd.Month > curDate.Month)
    {
        age--;
    }
    Console.WriteLine("You are: {0} years old", age);
}



回答4:


age does not exists here:

if (bd.Month > curDate.Month)
{
    age--;
}

int age should be declared out of if-else if you want to access it.

int age = 0;

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--;
}



回答5:


Move age outside the else block and assign it a default value :

int age = 0;

if(...)
{
...
}
else
{
...
}



回答6:


Variable age is declared only in else block and you try to use it in next if block, where isn't declared.



来源:https://stackoverflow.com/questions/23092403/name-age-does-not-exist-in-the-current-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!