栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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;


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/432992.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号