我知道这很可能是一个愚蠢的问题,但我是一名大学生,他是C#和面向对象编程的新手。我试图在别处找到答案,但我找不到任何有用的东西。调试器一直告诉我变量'cust_num在当前上下文中不存在'。如果有人能告诉我我做错了什么并让我觉得自己像个白痴,我会非常感激。谢谢!
string get_cust_num()
{
bool cust_num_valid = false;
while (!cust_num_valid)
{
cust_num_valid = true;
Console.Write("Please enter customer number: ");
string cust_num = Console.ReadLine();
if (cust_num == "000000" || !Regex.IsMatch(cust_num, @"^[0-9]+$") || cust_num.Length != 6)
{
cust_num_valid = false;
Console.WriteLine("Invalid customer number detected. Customer numbers must be a 6 digit positive integer (zeros will not work)");
}
}
return cust_num;
}
答案 0 :(得分:9)
在while
:
string cust_num = null;
while ...
然后在while内设置它:
cust_num = Console.ReadLine();
因为您试图>> 之后
return cust_num;
答案 1 :(得分:4)
C#中的每个变量都存在于范围中,该范围由花括号定义:
{
...
int x = 0;
...
x = x + 1; // <- legal
...
// <- x is defined up to here
}
x = x - 1; // <- illegal, providing there's no other "x" declared
在您的情况下,cust_num
受while {...}
限制。它必须考虑如果 cust_num_valid = true ,你的代码应返回什么值,并且没有
cust_num 。
while (!cust_num_valid)
{ // <- Scope of cust_num begins
cust_num_valid = true;
Console.Write("Please enter customer number: ");
string cust_num = Console.ReadLine();
if (cust_num == "000000" || !Regex.IsMatch(cust_num, @"^[0-9]+$") || cust_num.Length != 6)
{
cust_num_valid = false;
Console.WriteLine("Invalid customer number detected. Customer numbers must be a 6 digit positive integer (zeros will not work)");
}
} // <- Scope of cust_num ends
return cust_num; // <- out of scope
要修复代码,请将string cust_num = "";
放在 while
之外:
string cust_num = ""; // <- declaration
while (!cust_num_valid)
{
cust_num_valid = true;
Console.Write("Please enter customer number: ");
cust_num = Console.ReadLine(); // <- no new declaration: "string" is removed
if (cust_num == "000000" || !Regex.IsMatch(cust_num, @"^[0-9]+$") || cust_num.Length != 6)
{
cust_num_valid = false;
Console.WriteLine("Invalid customer number detected. Customer numbers must be a 6 digit positive integer (zeros will not work)");
}
}
return cust_num;
答案 2 :(得分:1)
您的return cust_num
语句超出了cust_num
的定义上下文。由于您在while
循环中定义了它,因此它仅存在于该范围内。你需要将它移出循环。
您定义的任何局部变量仅存在于封装它的大括号内(以及任何嵌套括号中)。
答案 3 :(得分:1)
您尝试将cust_num
返回到定义它的while
块范围之外。如果要返回它,则需要在while之外定义它,例如:
string get_cust_num()
{
bool cust_num_valid = false;
string cust_num = string.Empty;
while (!cust_num_valid)
{
cust_num_valid = true;
Console.Write("Please enter customer number: ");
cust_num = Console.ReadLine();
if (cust_num == "000000" || !Regex.IsMatch(cust_num, @"^[0-9]+$") || cust_num.Length != 6)
{
cust_num_valid = false;
Console.WriteLine("Invalid customer number detected. Customer numbers must be a 6 digit positive integer (zeros will not work)");
}
}
return cust_num;
}
答案 4 :(得分:1)
当在代码块中定义变量时,它被限制在该范围内(当然从变量声明开始;在声明之前不能使用它)。如果查看示例,变量在while块中定义,但在其外部使用。
string get_cust_num()
{
while ()
{
string cust_num = Console.ReadLine(); // cust_num scope starts here
if ()
{
}
} // cust_num scope ends here
return cust_num;
}
您需要在方法级别定义它才能使用它:
string get_cust_num()
{
string cust_num = Console.ReadLine(); // cust_num scope starts here
while ()
{
if ()
{
}
}
return cust_num;
} // cust_num scope ends here
答案 5 :(得分:0)
您似乎正在尝试返回cust_num
的值。为了返回cust_num
的值,需要在while循环之外声明与“return”语句相同的级别。
有关详细信息,请参阅此链接:http://msdn.microsoft.com/en-us/library/ms973875.aspx