Visual Studio 无法识别定义的变量

Visual Studio won't recognise defined variable

提问人:cindersinned 提问时间:8/27/2023 更新时间:8/27/2023 访问量:48

问:

我对此非常陌生,并且在 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# 字符串 if-statement 控制台应用程序

评论

3赞 tttony 8/27/2023
只需在顶部声明一次该变量,然后在块中重用它if
0赞 cindersinned 8/27/2023
呃,我就知道就这么简单。谢谢。
1赞 Olivier Jacot-Descombes 8/27/2023
您还可以查看如何在 switch 语句中使用 c# 元组值类型来简化冗长的 if 语句。还有:在 C# 8.0 中使用开关表达式和模式匹配

答:

0赞 Lajos Arpad 8/27/2023 #1

无法识别的不是 VS,而是 .NET。原因是它是代码块级别的范围(参见 https://www.tutorialsteacher.com/articles/variable-scopes-in-csharp),因此在创建它的代码块之外看不到它。解决方案是在它之外声明它,就在你的主之前,并确保你没有重新声明它,所以在内部情况下删除关键字,它应该很好。finalColorfinalColorifstring

0赞 BQF 8/27/2023 #2

当在一个块中定义一个变量时,所以不能用它到另一个块,因为它不是全局的。 更改后首先定义变量:

string finalColour="";
    

新代码:

string finalColour="";

    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)
    {
        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)
        {  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."; }
//error catcher

    Console.WriteLine("The creature has this pattern: " + PatternList[rndPat] + ", and its fur is this colour: " + finalColour + ".");
    Console.ReadKey();