2017-05-03 4 views
0

나는이 코딩 작업에 조금 익숙하다. 누구든지이 일을 첫 번째 선택으로 되돌릴 수있는 방법을 알려줄 수 있습니까? 선생님은 간단한 RPG 게임을 만들기를 원했고 약간의 문제가있어서 첫 번째 메뉴로 되돌릴 수 없습니다. 고마워요.RPG 게임 "뒤로"루프

package looptest; 
    import java.io.*; 

public class LoopTest { 
    public static BufferedReader br; 


    public static void main(String[] args) throws IOException{ 
     br = new BufferedReader (new InputStreamReader(System.in)); 

     // i want loop it back here when you press the back botton 
     System.out.println("What do you want to do?\n" 
       + "[1] Examine\n" 
       + "[2] Speak\n" 
       + "[3] Move"); 
     short choice = Short.parseShort(br.readLine()); 

     while(choice !=3)  

      switch (choice){ 
      case 1: 
       System.out.println("What do you want to examine?\n" 
         + "[1] Bed\n" 
         + "[2] Closet\n" 
         + "[3] Vase\n" 
         + "[4] back"); 
       short choice1 = Short.parseShort(br.readLine()); 
       switch (choice1){ 
        case 1: 
         System.out.println("What a nice bed"); 
         break; 
        case 2: 
         System.out.println("Better not touce the elder's things."); 
         break; 
        case 3: 
         System.out.println("This vase might break if i touched it "); 
         break; 
        case 4: 
        // loops back to the first menu 
         break; 
       } 

       break; 
      case 2: 
       System.out.println("Who do you want to speak to?\n" 
         + "[1] Maiden\n" 
         + "[2] Elder\n" 
         + "[3] Guard\n" 
         + "[4] Back"); 
       short choice2 = Short.parseShort(br.readLine()); 
       switch (choice2){ 
        case 1: 
         System.out.println("Hello there how are you feeling?\n" 
           + "you falling must be very painful i hope you get well soon."); 
         break; 
        case 2: 
         System.out.println("Shoku is waiting for you in his tent go to him he will teach\n" 
           + "you on how to fight. You will need it on your adventure."); 
         break; 
        case 3: 
         System.out.println("....*grunts* "); 
         break; 
        case 4: 
         // loops nack to the first menu 
         break; 
       } 
       break; 
      case 3: 
       break;    
     } 
         System.out.println("Where to you want to go?\n" 
         + "[1] Outside\n" 
         + "[2] Stay inside"); 
       short choice3 = Short.parseShort(br.readLine()); 
       if (choice3 == 1){ 
        System.out.println("Okey lets go "); 
       } 

    } 

} 
+0

아마도이 문제를 별도의 방법으로 해결해야합니다. 길고 길게 감에 따라 메뉴에 루프를 추가 할 곳을 찾는 데 어려움을 겪고 있습니다. – markspace

답변

0

그래서 게임 로직을 보유하고있는 모든 것을 별도의 방법으로 넣을 수 있습니다. 숫자 4를 입력 한 후 다음 만 return으로이 방법을 종료해야하고 모든 것을 다시 시작이 이어질 것이다, 다시 메소드를 호출

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

public class RPG { 

    private BufferedReader br; 

    private RPG() { 
     br = new BufferedReader(new InputStreamReader(System.in)); 
    } 

    public static void main(String[] args) throws IOException { 
     RPG rpg = new RPG(); 
     rpg.run(); 
    } 

    private void run() throws IOException { 
     System.out.println("What do you want to do?\n" 
       + "[1] Examine\n" 
       + "[2] Speak\n" 
       + "[3] Move"); 
     short choice = Short.parseShort(br.readLine()); 

     while (choice != 3) { 
      switch (choice) { 
       case 1: 
        System.out.println("What do you want to examine?\n" 
          + "[1] Bed\n" 
          + "[2] Closet\n" 
          + "[3] Vase\n" 
          + "[4] back"); 
        short choice1 = Short.parseShort(br.readLine()); 
        switch (choice1) { 
         case 1: 
          System.out.println("What a nice bed"); 
          break; 
         case 2: 
          System.out.println("Better not touce the elder's things."); 
          break; 
         case 3: 
          System.out.println("This vase might break if i touched it "); 
          break; 
         case 4: 
          run(); 
          return; 
        } 
        break; 
       case 2: 
        System.out.println("Who do you want to speak to?\n" 
          + "[1] Maiden\n" 
          + "[2] Elder\n" 
          + "[3] Guard\n" 
          + "[4] Back"); 
        short choice2 = Short.parseShort(br.readLine()); 
        switch (choice2) { 
         case 1: 
          System.out.println("Hello there how are you feeling?\n" 
            + "you falling must be very painful i hope you get well soon."); 
          break; 
         case 2: 
          System.out.println("Shoku is waiting for you in his tent go to him he will teach\n" 
            + "you on how to fight. You will need it on your adventure."); 
          break; 
         case 3: 
          System.out.println("....*grunts* "); 
          break; 
         case 4: 
          run(); 
          return; 
        } 
        break; 
      } 
     } 
     System.out.println("Where to you want to go?\n" 
       + "[1] Outside\n" 
       + "[2] Stay inside"); 
     short choice3 = Short.parseShort(br.readLine()); 

     if (choice3 == 1) { 
      System.out.println("Okey lets go "); 
     } 
    } 
} 

BTW : 메인 스위치에 규정 된 경우를 이 스위치를 구현할 필요가 없으므로 choise가 3이 아닌 동안이 스위치를 호출 할 수 있습니다.

0

모든 선택 항목에 모두 while -loop을 넣습니다. 기본적으로 각각의 선택은 새로운 while -loop입니다. 이처럼

:

while (choice != 4) { 
    // Write the choices 
    // Read the input from user and move on to next, unless the choice 
    // is to exit (either the game or "go back") 

    switch (choice) { 
    case 0: 
     // meh 
     break; 
    case 1: 
     // meh 
    case 2: 
     while (choice != 1) { 
      // Write the new choices 
      // Read the input.......... 

      switch (choice) { 
      case 0: 
       while (choice != 0) { 
        // We can keep doing this, and every time we go 
        // "further in", we can go back by pressing e.g. 4, or 
        // whatever option is "back". 
        // When going back, we'll go back to previous loop, 
        // and it'll re-print the choices 
       } 
       break; 
      case 1: 
       System.out.println("back!"); 
      } 
     } 
     break; 
    case 3: 
     // meh 
    case 4: 
     System.out.println("back"); 
     break; 
    } 
} 

나는 그것이 의미가 있기를 바랍니다.