C# 不可达的goto语句

C# 不可达的goto语句,c#,C#,我正在用这个基于文本的游戏做这个测试,检查键和检查门转到开关不工作,它说是无法访问的代码 为什么会这样 action: Console.WriteLine("what do you want to do"); string actionAnswer = Console.ReadLine(); inspectSuroundings: Console.WriteLine("You see a small white room with a l

我正在用这个基于文本的游戏做这个测试,检查键和检查门转到开关不工作,它说是无法访问的代码 为什么会这样

action:
        Console.WriteLine("what do you want to do");
        string actionAnswer = Console.ReadLine();

    inspectSuroundings:
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
        goto action;

    inspectKey:
        Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
        goto action;

    inspectDoor:
        Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
        goto action;

        if ((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
        {
            goto inspectSuroundings;
        }else if ((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
        {
            goto inspectKey;
        }else if ((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
        {
            goto inspectDoor;
        }else
        {
            Console.Beep();
            goto action;
        }

您正在使用
转到操作
InspectSurounds:
中,这使以下代码无法访问

action:
        Console.WriteLine("what do you want to do");
        string actionAnswer = Console.ReadLine();

    inspectSuroundings:
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
        goto action;

    inspectKey:
        Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
        goto action;

    inspectDoor:
        Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
        goto action;

        if ((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
        {
            goto inspectSuroundings;
        }else if ((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
        {
            goto inspectKey;
        }else if ((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
        {
            goto inspectDoor;
        }else
        {
            Console.Beep();
            goto action;
        }
inspectKey:
        Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
        goto action;

    inspectDoor:
        Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
        goto action;

        if ((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
        {
            goto inspectSuroundings;
        }else if ((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
        {
            goto inspectKey;
        }else if ((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
        {
            goto inspectDoor;
        }else
        {
            Console.Beep();
            goto action;
        }

我建议您使用面向对象编程(OOP)的方法来编写这个程序,它不仅可以帮助您理解和调试程序,还可以开发一个干净、不易出错的程序

问题在于你的逻辑!以下行是唯一执行的行

action:
        Console.WriteLine("what do you want to do");
        string actionAnswer = Console.ReadLine();

    inspectSuroundings:
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
        goto action;

    inspectKey:
        Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
        goto action;

    inspectDoor:
        Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
        goto action;

        if ((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
        {
            goto inspectSuroundings;
        }else if ((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
        {
            goto inspectKey;
        }else if ((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
        {
            goto inspectDoor;
        }else
        {
            Console.Beep();
            goto action;
        }
action:
        Console.WriteLine("what do you want to do");
        string actionAnswer = Console.ReadLine();

    inspectSuroundings:
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
        goto action;
在这一行之后,它将继续转到动作标签。因此,代码的另一部分将不会执行

action:
        Console.WriteLine("what do you want to do");
        string actionAnswer = Console.ReadLine();

    inspectSuroundings:
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
        goto action;

    inspectKey:
        Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
        goto action;

    inspectDoor:
        Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
        goto action;

        if ((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
        {
            goto inspectSuroundings;
        }else if ((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
        {
            goto inspectKey;
        }else if ((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
        {
            goto inspectDoor;
        }else
        {
            Console.Beep();
            goto action;
        }
action:
        Console.WriteLine("what do you want to do");
        string actionAnswer = Console.ReadLine();

    inspectSuroundings:
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
        goto action;
此外,建议不要在C#中使用goto和label。它们可以替换为条件操作

对后藤说不
action:
        Console.WriteLine("what do you want to do");
        string actionAnswer = Console.ReadLine();

    inspectSuroundings:
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
        goto action;

    inspectKey:
        Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
        goto action;

    inspectDoor:
        Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
        goto action;

        if ((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
        {
            goto inspectSuroundings;
        }else if ((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
        {
            goto inspectKey;
        }else if ((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
        {
            goto inspectDoor;
        }else
        {
            Console.Beep();
            goto action;
        }

此代码块导致此错误

action:
        Console.WriteLine("what do you want to do");
        string actionAnswer = Console.ReadLine();

    inspectSuroundings:
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
        goto action;

    inspectKey:
        Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
        goto action;

    inspectDoor:
        Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
        goto action;

        if ((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
        {
            goto inspectSuroundings;
        }else if ((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
        {
            goto inspectKey;
        }else if ((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
        {
            goto inspectDoor;
        }else
        {
            Console.Beep();
            goto action;
        }

action:
        Console.WriteLine("what do you want to do");
        string actionAnswer = Console.ReadLine();

    inspectSuroundings:
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
        goto action;

    inspectKey:
        Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
        goto action;

    inspectDoor:
        Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
        goto action;

        if ((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
        {
            goto inspectSuroundings;
        }else if ((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
        {
            goto inspectKey;
        }else if ((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
        {
            goto inspectDoor;
        }else
        {
            Console.Beep();
            goto action;
        }
它将只在这两个时间段之间进行迭代。它将到达第22行,然后返回第16行执行操作:标签

action:
        Console.WriteLine("what do you want to do");
        string actionAnswer = Console.ReadLine();

    inspectSuroundings:
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
        goto action;

    inspectKey:
        Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
        goto action;

    inspectDoor:
        Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
        goto action;

        if ((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
        {
            goto inspectSuroundings;
        }else if ((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
        {
            goto inspectKey;
        }else if ((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
        {
            goto inspectDoor;
        }else
        {
            Console.Beep();
            goto action;
        }
注: 在继续之前阅读这些

action:
        Console.WriteLine("what do you want to do");
        string actionAnswer = Console.ReadLine();

    inspectSuroundings:
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
        goto action;

    inspectKey:
        Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
        goto action;

    inspectDoor:
        Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
        goto action;

        if ((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
        {
            goto inspectSuroundings;
        }else if ((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
        {
            goto inspectKey;
        }else if ((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
        {
            goto inspectDoor;
        }else
        {
            Console.Beep();
            goto action;
        }

action:
        Console.WriteLine("what do you want to do");
        string actionAnswer = Console.ReadLine();

    inspectSuroundings:
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
        goto action;

    inspectKey:
        Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
        goto action;

    inspectDoor:
        Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
        goto action;

        if ((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
        {
            goto inspectSuroundings;
        }else if ((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
        {
            goto inspectKey;
        }else if ((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
        {
            goto inspectDoor;
        }else
        {
            Console.Beep();
            goto action;
        }

action:
        Console.WriteLine("what do you want to do");
        string actionAnswer = Console.ReadLine();

    inspectSuroundings:
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
        goto action;

    inspectKey:
        Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
        goto action;

    inspectDoor:
        Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
        goto action;

        if ((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
        {
            goto inspectSuroundings;
        }else if ((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
        {
            goto inspectKey;
        }else if ((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
        {
            goto inspectDoor;
        }else
        {
            Console.Beep();
            goto action;
        }
goto==编程错误

action:
        Console.WriteLine("what do you want to do");
        string actionAnswer = Console.ReadLine();

    inspectSuroundings:
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
        goto action;

    inspectKey:
        Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
        goto action;

    inspectDoor:
        Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
        goto action;

        if ((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
        {
            goto inspectSuroundings;
        }else if ((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
        {
            goto inspectKey;
        }else if ((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
        {
            goto inspectDoor;
        }else
        {
            Console.Beep();
            goto action;
        }
重嵌套循环==更糟糕的编程

action:
        Console.WriteLine("what do you want to do");
        string actionAnswer = Console.ReadLine();

    inspectSuroundings:
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
        goto action;

    inspectKey:
        Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
        goto action;

    inspectDoor:
        Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
        goto action;

        if ((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
        {
            goto inspectSuroundings;
        }else if ((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
        {
            goto inspectKey;
        }else if ((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
        {
            goto inspectDoor;
        }else
        {
            Console.Beep();
            goto action;
        }
不要使用goto。。。修正你的代码

action:
        Console.WriteLine("what do you want to do");
        string actionAnswer = Console.ReadLine();

    inspectSuroundings:
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
        goto action;

    inspectKey:
        Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
        goto action;

    inspectDoor:
        Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
        goto action;

        if ((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
        {
            goto inspectSuroundings;
        }else if ((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
        {
            goto inspectKey;
        }else if ((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
        {
            goto inspectDoor;
        }else
        {
            Console.Beep();
            goto action;
        }

您应该在标签前写下条件。因此,您的程序看起来像

action:
        Console.WriteLine("what do you want to do");
        string actionAnswer = Console.ReadLine();

    inspectSuroundings:
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
        goto action;

    inspectKey:
        Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
        goto action;

    inspectDoor:
        Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
        goto action;

        if ((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
        {
            goto inspectSuroundings;
        }else if ((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
        {
            goto inspectKey;
        }else if ((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
        {
            goto inspectDoor;
        }else
        {
            Console.Beep();
            goto action;
        }
action:
    Console.WriteLine("what do you want to do");
    string actionAnswer = Console.ReadLine();


    if((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
    {
        goto inspectSuroundings;
    }
    else if((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
    {
        goto inspectKey;
    }
    else if((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
    {
        goto inspectDoor;
    }
    else
    {
        Console.Beep();
        goto action;
    }

inspectSuroundings:
    Console.WriteLine("You see a small white room with a large two pronged key and a door.");
    goto action;

inspectKey:
    Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
    goto action;

inspectDoor:
    Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
    goto action;
但这并不是解决你遇到的问题的方法,你需要做一个循环,而不是去做。您可以阅读更多有关的信息,从而使您的程序

action:
        Console.WriteLine("what do you want to do");
        string actionAnswer = Console.ReadLine();

    inspectSuroundings:
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
        goto action;

    inspectKey:
        Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
        goto action;

    inspectDoor:
        Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
        goto action;

        if ((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
        {
            goto inspectSuroundings;
        }else if ((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
        {
            goto inspectKey;
        }else if ((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
        {
            goto inspectDoor;
        }else
        {
            Console.Beep();
            goto action;
        }
while(true)
{
    Console.WriteLine("what do you want to do");
    string actionAnswer = Console.ReadLine();
    if((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
    {
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
    }
    else if((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
    {
        Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
    }
    else if((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
    {
        Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
    }
    else
    {
        Console.Beep();
    }
}

不要使用
goto
。永远不要使用
goto
。我看到的是,你的代码就像一个循环
操作:Console.WriteLine(“你想做什么”);字符串actionAnswer=Console.ReadLine();检查环境:Console.WriteLine(“你看到一个白色的小房间,有一把大的双刃钥匙和一扇门。”);后藤行动@Prajwal goto有一些用法,因此该建议无效。但通常不需要。至于问题:逐行调试,您将看到。在这种情况下,不要使用goto并编写正确的代码,例如,使用switch/case时,没有条件忽略
got操作所以它总是将其重定向到label@SamiKuhmonen,我知道。但是对于这个级别的人来说,最好是建议反对。而且,
goto
使代码的可读性大大降低**我的意见