2013-12-21 35 views
1

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

当用户按错键,岂不是更好,如果你只是问再次输入特定的按键? –

+0

你不能在一个函数中设置你的代码,并一次又一次地调用它,直到用户按下正确的键?这就是为什么函数存在的原因 – Sugar

+0

不要向我们展示该代码(将其包装到函数中,无论是在示例中还是在实际代码中),向我们展示您将如何使用它。 –

回答

0

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

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

0

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

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; 

}