gpt4 book ai didi

c# - 当前上下文中不存在变量?

转载 作者:太空狗 更新时间:2023-10-29 20:52:57 25 4
gpt4 key购买 nike

我知道这很可能是一个愚蠢的问题,但我是一名刚刚接触 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;
}

最佳答案

while 之外定义它:

string cust_num = null;
while ...

然后在 while 中像这样设置它:

cust_num = Console.ReadLine();

因为您正尝试在 while 之后访问它:

return cust_num;

关于c# - 当前上下文中不存在变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18233630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com