2013-12-21 6 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

, 당신은 또한

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

을 방법을 사용할 수 있습니다

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`. */ 
} 

while 루프를 사용할 수 있습니다 그리고 당신은 클래스

에 다른 곳에서 그 호출 할 수 있습니다
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/13940fs2.aspx

0

한 대답은

http://msdn.microsoft.com/en-us/library/d96yfwee.aspx이 같은 루프에서 유효한 입력이 있는지 확인하기 :

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; 

} 사용자가 잘못된 키를 누르면 방금 다시 특정 키를 입력하도록 요청하는 경우