我知道这可能是一个愚蠢的问题,但我是一名刚开始学习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;
}
string cust_num;
(不定义= null
),并使用do { } while()
循环(因为该循环至少会执行一次)。 - Nolonar