回到一段代码的开头?

时间:2013-12-21 18:40:45

标签: c# return

我需要在用户输入错误的密钥后返回到此处显示的代码的开头。是否有任何简单的代码行将返回另一行?正如您所看到的,我已经设置了if语句,因此我可以添加一些可以返回到我的代码中的开头或其他区域的内容。我对c#和一般的编程真的很新。我真的只是不想再将所有代码输入到另一个会产生相同问题的if语句中。我最好在用户输入错误的密钥后再次运行代码,因为这样他们就可以重新读取它而无需再次从头开始。

//Runs battle interactive
Console.WriteLine("");
Console.WriteLine("You have encountered a simple guard!  He deals 2 damage per attack and has 1 HP.");
Console.WriteLine("You currently have: " + Program.Inventory);
Console.WriteLine("Choose a weapon!");
var input2 = Console.ReadKey();


//Key checker for items
switch (input2.Key)
{
    case ConsoleKey.D1:
        Console.WriteLine("");
        if (Items.iniFists == true)
        {
            Console.WriteLine("You have attacked with your Fists for 1 DMG!");
        }
        else
        {
            //this will never run, just a placeholder
            Console.WriteLine("You Don't have your fists!");
            switch (input2.Key)
            {
                case ConsoleKey.D1:
                    Console.WriteLine("");
                    if (Items.iniFists == true)
                    {
                        Console.WriteLine("You have attacked with your Fists for 1 DMG!");
                    }
                    else
                    {
                        //this will never run, just a placeholder
                        Console.WriteLine("You Don't have your fists!");
                    }
                    break;
                case ConsoleKey.D2:
                    Console.WriteLine("");
                    if (Items.iniLongsword == true)
                    {
                        Console.WriteLine("You have chosen to attack with the Longsword for 2 DMG!");
                    }
                    else
                    {
                        Console.WriteLine("You don't have a longsword!");
                    }
                    break;
                case ConsoleKey.D3:
                    Console.WriteLine("");
                    if (Items.iniBow == true)
                    {
                        Console.WriteLine("You have chosen to attack with the Bow for 3 DMG!");
                    }
                    else
                    {
                        Console.WriteLine("You don't have a Bow!");
                    }
                    break;
                case ConsoleKey.D4:
                    Console.WriteLine("");
                    if (Items.iniLightstaff == true)
                    {
                        Console.WriteLine("You have chosen to attack with the Lightstaff for 4 DMG!");
                    }
                    else
                    {
                        Console.WriteLine("You don't have a Lightstaff!");
                    }
                    break;
                case ConsoleKey.D5:
                    Console.WriteLine("");
                    Console.WriteLine("You can't attack with an Apple!");
                    break;
                case ConsoleKey.D6:
                    Console.WriteLine("");
                    Console.WriteLine("You can't attack with a Golden Key!");
                    break;
                case ConsoleKey.D7:
                    Console.WriteLine("");
                    Console.WriteLine("You can't attack with a Steak!");
                    break;
            }
        }
        break;
    case ConsoleKey.D2:
        Console.WriteLine("");
        if (Items.iniLongsword == true)
        {
            Console.WriteLine("You have chosen to attack with the Longsword for 2 DMG!");
        }
        else
        {
            Console.WriteLine("You don't have a longsword!");
        }
        break;
    case ConsoleKey.D3:
        Console.WriteLine("");
        if (Items.iniBow == true)
        {
            Console.WriteLine("You have chosen to attack with the Bow for 3 DMG!");
        }
        else
        {
            Console.WriteLine("You don't have a Bow!");
        }
        break;
    case ConsoleKey.D4:
        Console.WriteLine("");
        if (Items.iniLightstaff == true)
        {
            Console.WriteLine("You have chosen to attack with the Lightstaff for 4 DMG!");
        }
        else
        {
            Console.WriteLine("You don't have a Lightstaff!");
        }
        break;
    case ConsoleKey.D5:
        Console.WriteLine("");
        Console.WriteLine("You can't attack with an Apple!");
        break;
    case ConsoleKey.D6:
        Console.WriteLine("");
        Console.WriteLine("You can't attack with a Golden Key!");
        break;
    case ConsoleKey.D7:
        Console.WriteLine("");
        Console.WriteLine("You can't attack with a Steak!");
        break;
}

3 个答案:

答案 0 :(得分:3)

C#支持代码中的标签,但不推荐使用它,因为它违反了许多编码最佳实践,但我想任何规则都有例外。

class Program
{
    static void Main(string[] args)
    {
    Start:
        Console.WriteLine("Start Here... Press any key");
        var key = Console.ReadKey(true);

        switch (key.Key)
        {
            case ConsoleKey.A:
                goto MyLabel;

            case ConsoleKey.B:
                goto MyLabel2;

            case ConsoleKey.C:
                goto MyLabel3;

            default:
                Console.WriteLine("Bad Choice");
                goto Start;

        }

    MyLabel:
        Console.WriteLine("MyLabel: A");
        goto Start;

    MyLabel2:
        Console.WriteLine("MyLabel: B");
        goto Start;


    MyLabel3:
        Console.WriteLine("MyLabel: C");
        goto Start;
    }
}

您可以在此处找到更多信息:

http://msdn.microsoft.com/en-us/library/d96yfwee.aspx http://msdn.microsoft.com/en-us/library/13940fs2.aspx

答案 1 :(得分:1)

你有几个选项,你可以使用while循环

bool continue = true;
while(continue == true)// or you can simply type "while(continue)"
{
    /* everything inside the `while` loop will be 
       repeated until `continue` is not `true`. */
}

你也可以使用方法

public static void doStuff()
{
    // insert stuff here
}

然后你可以从班级的其他地方拨打电话

if(x = 6)
{
    doStuff();  //this line does the stuff
    doStuff();  // this line does the stuff again.
}

答案 2 :(得分:1)

对此的一个答案是检查在这样的循环中是否有有效输入:

while (true)
{
    ConsoleKey i = Console.ReadKey()
    if (i == ConsoleKey.D1 || ... ) //Check if it's equal to any valid key, you 
                                    //might be able to simplify it with <= and 
                                    //>= if valid keys are sequential.
        break;
    Console.WriteLine("You have entered an invalid key");
}

或者,您可以在切换块的末尾添加goto语句:

SwitchStatement: switch(input2.Key)
...
default:
    Console.WriteLine("Invalid key pressed");

    goto SwitchStatement;
    break;

}

相关问题