我正在编写一个小的控制台程序,要求输入姓名和年龄,然后确定您的年龄足以继续进行。我尝试使用变量来确定该人是否可以继续:
Bool door = true;
然后,我想问那些因年龄而被拒绝的人是否想再次填写。
if (door == false){
Console.WriteLine("Wilt u op nieuw uw leeftijd invullen?");
}
但是Visual Studio给我以下错误:
错误CS0103在当前上下文中不存在名称“门”
这是完整的代码:
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#中定义一个范围。这意味着在该区域中声明的所有内容都是该区域的本地内容。这意味着您执行以下操作:
{
Console.WriteLine("U mag net door");
bool door = true;
Console.ReadLine();
}
door
变量所在的区域受封闭的花括号限制,在其他任何地方均不存在。之后,当您尝试在if (door == false)
中某个不存在且从未存在过名为door
的变量的地方使用它时,编译器基本上会告诉您。您将需要事先声明变量,例如
bool door = false;
if (Age < 18){
//....
} else {
Console.WriteLine("U bent door naar de volgende ronde");
door = true;
Console.ReadLine();
}
才能在整个程序中使用它。
关于c# - 该名称门在当前上下文中不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33127885/