【问题标题】:Returning back to the beginning of a block of code?回到代码块的开头?
【发布时间】:2013-12-21 18:40:44
【问题描述】:

在用户输入错误的键后,我需要返回此处显示的代码的开头。是否有任何简单的代码行会返回到另一行?如您所见,我已经设置了一个 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;
}

【问题讨论】:

  • 当用户按错键时,再要求输入该特定键不是更好吗?
  • 你不能在一个函数中设置你的代码并一次又一次地调用它,直到用户按下正确的键?这就是函数存在的原因
  • 不要向我们展示那个代码(在示例和您的实际代码中将其包装成一个函数),向我们展示您(将)如何使用它。
  • 请尽量减少代码,只显示代码的相关部分。对于一行问题,理解整个代码太复杂了。
  • 复杂?怎么复杂?超过需要是的,复杂的......不。

标签: c# return


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    你有几个选择,你可以使用 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.
    }
    

    【讨论】:

      【解决方案3】:

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

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

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

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

      }

      【讨论】:

        猜你喜欢
        • 2013-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-22
        • 1970-01-01
        • 1970-01-01
        • 2016-08-12
        • 1970-01-01
        相关资源
        最近更新 更多