RPG游戏"返回"环

时间:2017-05-03 14:42:35

标签: java

我对这些编码有点新鲜。任何人都可以帮助我如何将这个东西循环回到第一选择。老师希望我们制作一个简单的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 ");
                }

    }

}

2 个答案:

答案 0 :(得分: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 ");
        }
    }
}

顺便说一句:主switch语句中的情况是无法访问的,所以你不必实现,当choise不等于3时,可以调用此开关。

答案 1 :(得分:0)

将所有内容放入while循环中以供每个选择使用;基本上每个选项都是一个新的while - 循环。

像这样:

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

我希望这是有道理的。