Visual Studio无法识别C# if/else语句中定义的finalColour变量
问题描述
我是编程新手,用C#写程序时遇到了问题:在if/else的各个条件分支里定义了string类型变量finalColour,但在分支外的Console.WriteLine里使用它时,Visual Studio提示错误“The name 'finalColour does not exist in the current context'”,没法编译测试。代码如下:
using System; using System.Runtime.CompilerServices; //built in the most recent visual studio with c# { Console.WriteLine("This is just a basic generator, more will be added later."); //defining colours and patterns string[] ColourList = new string[] { "Red", "Yellow", "Blue", "White", "Black" }; string[] PatternList = new string[] { "Plain", "Spotted", "Striped", "Royal", "Patchy" }; //picking random colours and patterns Random rnd = new Random(); int rndClr1 = rnd.Next(ColourList.Length); int rndClr2 = rnd.Next(ColourList.Length); int rndPat = rnd.Next(PatternList.Length); //think i had to do this to make the first part of the if/else work, not sure why string Colour1 = rndClr1.ToString(); string Colour2 = rndClr2.ToString(); if (Colour1 == Colour2) { string finalColour = Colour1; //if the two colours are the same, then that's the final colour, otherwise mix them using the following rules } else if (rndClr1 == 0) { if (rndClr2 == 1) { string finalColour = "Orange"; } else if (rndClr2 == 2) { string finalColour = "Purple"; } else if (rndClr2 == 3) { string finalColour = "Pink"; } else { string finalColour = "Dark Red"; } } else if (rndClr1 == 1) { if (rndClr2 == 2) { string finalColour = "Green"; } else if (rndClr2 == 3) { string finalColour = "Pale Yellow"; } else if (rndClr2 == 4) { string finalColour = "Brown"; } else { string finalColour = "Orange"; } } else if (rndClr1 == 2) { if (rndClr2 == 0) { string finalColour = "Purple"; } else if (rndClr2 == 1) { string finalColour = "Green"; } else if (rndClr2 == 3) { string finalColour = "Sky Blue"; } else { string finalColour = "Navy"; } } else if (rndClr1 == 3) { if (rndClr2 == 0) { string finalColour = "Pink"; } else if (rndClr2 == 1) { string finalColour = "Pale Yellow"; } else if (rndClr2 == 2) { string finalColour = "Sky Blue"; } else { string finalColour = "Grey"; } } else if (rndClr1 == 4) { if (rndClr2 == 0) { finalColour = "Dark Red"; } else if (rndClr2 == 1) { finalColour = "Brown"; } else if (rndClr2 == 2) { finalColour = "Navy"; } else { finalColour = "Grey"; } } else { string finalColour = "Well, I don't know how this happened, but the colour generator broke."; } //error catcher Console.WriteLine("The creature has this pattern: " + PatternList[rndPat] + ", and its fur is this colour: " + finalColour + "."); Console.ReadKey(); //for some reason, vs doesn't recognise finalColour as a variable that's already been defined, so the testing fails //i don't know why }
错误原因
- 你在每个if/else分支的大括号内部重新定义了
finalColour,C#中变量的作用域仅限于它被定义的代码块({}包裹的范围)内,分支外的代码根本访问不到这些局部变量,因此编译时会提示变量不存在的错误。 - 另外代码里还有个逻辑错误:
Colour1 = rndClr1.ToString()是把随机索引转成字符串(比如索引0变成"0"),但你实际想判断的是两个随机选中的颜色是否相同,应该直接比较rndClr1 == rndClr2(索引相同则颜色相同),或者比较ColourList[rndClr1] == ColourList[rndClr2],而不是索引的字符串形式。
解决方法
- 提前声明变量:在所有if/else分支之前声明
finalColour,只在分支内部给它赋值,不要重新定义变量。 - 修正颜色判断逻辑:用索引直接判断是否相等,或者比较对应的颜色名称,确保逻辑符合预期。
修改后的完整代码:
using System; { Console.WriteLine("This is just a basic generator, more will be added later."); //defining colours and patterns string[] ColourList = new string[] { "Red", "Yellow", "Blue", "White", "Black" }; string[] PatternList = new string[] { "Plain", "Spotted", "Striped", "Royal", "Patchy" }; //picking random colours and patterns Random rnd = new Random(); int rndClr1 = rnd.Next(ColourList.Length); int rndClr2 = rnd.Next(ColourList.Length); int rndPat = rnd.Next(PatternList.Length); // 提前声明变量,作用域覆盖后续所有分支和输出语句 string finalColour = ""; // 直接比较索引,判断是否选中了同一个颜色 if (rndClr1 == rndClr2) { finalColour = ColourList[rndClr1]; } else if (rndClr1 == 0) { if (rndClr2 == 1) { finalColour = "Orange"; } else if (rndClr2 == 2) { finalColour = "Purple"; } else if (rndClr2 == 3) { finalColour = "Pink"; } else { finalColour = "Dark Red"; } } else if (rndClr1 == 1) { if (rndClr2 == 2) { finalColour = "Green"; } else if (rndClr2 == 3) { finalColour = "Pale Yellow"; } else if (rndClr2 == 4) { finalColour = "Brown"; } else { finalColour = "Orange"; } } else if (rndClr1 == 2) { if (rndClr2 == 0) { finalColour = "Purple"; } else if (rndClr2 == 1) { finalColour = "Green"; } else if (rndClr2 == 3) { finalColour = "Sky Blue"; } else { finalColour = "Navy"; } } else if (rndClr1 == 3) { if (rndClr2 == 0) { finalColour = "Pink"; } else if (rndClr2 == 1) { finalColour = "Pale Yellow"; } else if (rndClr2 == 2) { finalColour = "Sky Blue"; } else { finalColour = "Grey"; } } else if (rndClr1 == 4) { if (rndClr2 == 0) { finalColour = "Dark Red"; } else if (rndClr2 == 1) { finalColour = "Brown"; } else if (rndClr2 == 2) { finalColour = "Navy"; } else { finalColour = "Grey"; } } else { finalColour = "Well, I don't know how this happened, but the colour generator broke."; } Console.WriteLine("The creature has this pattern: " + PatternList[rndPat] + ", and its fur is this colour: " + finalColour + "."); Console.ReadKey(); }
内容的提问来源于stack exchange,提问作者cindersinned
相关产品推荐
相关产品推荐

