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

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

问题描述

我正在编写一个小的控制台程序,要求输入姓名和年龄,然后确定您的年龄是否足以继续.我尝试使用变量来确定该人是否可以继续:

I am writing a little console program that asks for name and age and then determines if you're old enough to proceed. I tried using a variable to determine if the person could proceed:

Bool door = true;

然后我想问那些因年龄而被拒绝的人是否想再次填写.

Then I wanted to ask the persons who were denied because of their age if they want to fill it in again.

if (door == false){
  Console.WriteLine("Wilt u op nieuw uw leeftijd invullen?");
}

但是Visual Studio给我以下错误:

But Visual Studio gives me the following error:

错误CS0103在当前上下文ConsoleApplication1中不存在名称门"

Error CS0103 The name 'door' does not exist in the current context ConsoleApplication1

这是完整的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Hallo, wie ben jij? ");
            string Name = Convert.ToString (Console.ReadLine());
            Console.Write("Hallo " + Name + ", hoe oud ben jij? ");
            int Age = Convert.ToInt32 (Console.ReadLine());
            Console.ReadLine();

            if (Age < 18) {
                int remaining = 18 - Age;
                Console.WriteLine("Helaas bent u nog geen achttien, U moet nog:       "+remaining+" Jaar wachten.");
                Console.ReadLine();
            } else if (Age == 18) {
                Console.WriteLine("U mag net door");
                bool door = true;
                Console.ReadLine();
            } else {
                Console.WriteLine("U bent door naar de volgende ronde");
                bool door = true;
                Console.ReadLine();
            }

            if (door == false)
            {
                Console.WriteLine("Wilt u op nieuw uw leeftijd invullen?");
            }
        }
    }
}

推荐答案

每对花括号 {...} 在C#中定义了一个 scope .这意味着在该区域中声明的所有内容都是该区域的本地内容.这就是说,当您这样做时:

Each pair of curly braces { ... } defines a scope in C#. That means that everything declared in that area is local to that area. That means that when you do:

{
  Console.WriteLine("U mag net door");
  bool door = true;
  Console.ReadLine();
}

door 变量所在的区域受封闭的花括号限制,并且在其他任何地方均不存在.之后,当您尝试使用它时,在 if(door == false)中,在一个不存在且从未存在的名为 door 的变量的地方,编译器基本上告诉你了.

the area where the door variable lives, is restricted with the closed curly brace, and it does not exist anywhere else. When you try to use it afterwards, in if (door == false), in a place where a variable called door does not exist, and has never existed, the compiler basically tells you that.

您需要预先声明变量,例如

You will need to declare your variable beforehand, something like

bool door = false;
if (Age < 18){
  //....
} else {
   Console.WriteLine("U bent door naar de volgende ronde");
   door = true;
   Console.ReadLine();
}

能够在整个程序中使用它.

to be able to use it throughout your program.

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

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