无法访问的goto语句

时间:2016-11-25 06:02:45

标签: c#

我正在使用这个基于文本的游戏进行此测试并且检查键和检查门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;
        }

4 个答案:

答案 0 :(得分:1)

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

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。它们可以用条件操作代替。

答案 1 :(得分:0)

您在goto action;中使用了inspectSuroundings:,这使得以下代码无法访问。

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)方法来编写这个程序,它不仅可以帮助你理解和调试程序,还可以帮助你开发一个干净,不易出错的程序。

答案 2 :(得分:0)

拒绝GOTO

此代码块导致此

enter image description here

    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;

它只会在这之间迭代。它将到达第22行,然后将返回第16行以执行操作:标签。

注意:

在继续之前阅读这些

GOTO still considered harmful?

http://www.drdobbs.com/jvm/programming-with-reason-why-is-goto-bad/228200966

  

goto ==编程错误

     

重度嵌套循环==更糟糕的编程

     

不要使用goto ...修复你的代码!

enter image description here

答案 3 :(得分:0)

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

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;

但是这不是解决你想出的情况的方法,你需要采取循环而不是制作GoTo。您可以阅读有关'goto' statement is bad practice的更多信息,这样您就可以将您的程序设为

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();
    }
}