Visual Studio 无法识别定义的变量

问题描述 投票:0回答:1

我对此很陌生,并且在 C# 中的 if/else 语句中遇到了变量问题。我想将涉及此变量的行打印到控制台,但 Visual Studio 无法识别该变量已定义,因此它不起作用。

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)
        { string finalColour = "Dark Red"; }
        else if (rndClr2 == 1)
        { string finalColour = "Brown"; }
        else if (rndClr2 == 2)
        { string finalColour = "Navy"; }
        else { string 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
}

理论上,finalColour 应该是一个可用的字符串,可以成为打印句子的一部分。然而,VS 一直说“名称‘finalColour 在当前上下文中不存在’,这是一个阻止测试/编译的错误。我不知道为什么!我觉得我错过了一些明显的东西,但我是新的编码能力足够强,我根本不知道那东西是什么。有经验更丰富的人有什么想法吗?

c# string if-statement console-application
1个回答
0
投票

不是VS不识别

finalColor
而是.NET。原因是
finalColor
是代码块级作用域(参见 https://www.tutorialsteacher.com/articles/variable-scopes-in-csharp),因此在代码块之外看不到它。已经被创造了。解决方案是在其外部声明它,就在主
if
之前,并确保不会重新声明它,因此在内部情况下删除
string
关键字,它应该很好。

© www.soinside.com 2019 - 2024. All rights reserved.