代码似乎在某一行之后没有继续

Code doesn't seem to continue after a certain line

提问人:mysterado 提问时间:10/19/2023 更新时间:10/19/2023 访问量:55

问:

我一直在尝试编写一个基于文本的视频游戏,发现一旦角色进入游戏的某个部分,就会遇到障碍。

这是来的代码。

while (true) {
                input = userInput.nextLine().toUpperCase();
                switch (input) {
                case "R":
                    System.out.println("You encounter an old man who greets you \n"
                            + "[OLD MAN] 'Hello there my boy! I've been expecting you for a long time! Where have you been?' \n"
                            + "How do you respond? \n"
                            + "\n"
                            + "[P] (Pretend to know the old man) Hello, my old friend! Long time no see, how are you? \n"
                            + "[C] (Curious I'm sorry, you must have me mistaken for someone else. I don't know you? \n"
                            + "[S] (Stay silent) \n"
                            + "\n"
                            + "[PLEASE ENTER EITHER [P], [C] OR [S]");
                    while (true) {
                        input = userInput.nextLine().toUpperCase();
                        switch (input) {
                        case "P":
                            System.out.println("[OLD MAN] 'I'm alive, I found that stick you were talking about. It was very hard to find. Had to pay an arm and a leg, literally.'"
                                    + "\nThe old man looks puzzled, as if you'd forgotten something"
                                    + "\n[OLD MAN] 'Do you not have what I asked for? \n'"
                                    + "\n"
                                    + "[P] (Pretending) I did, but I forgot it at home!\n"
                                    + "[C] (Confused) I don't have it, sorry! I don't remember what you wanted.\n"
                                    + "[S] (Stay silent) \n");
                            while (true) {
                                input = userInput.nextLine().toUpperCase();
                                switch (input) {
                                case "P":
                                case "C":
                                case "S":
                                    System.out.println("[OLD MAN] 'Oh, I understand. Well, hopefully you remember our deal; you have to guess the number I'm thinking of now. It's between 1 and 3.'");
                                    // Play the random number guessing game
                                    playRandomNumberGuesser(1, 3, userInput);
                                    break;
                                default:
                                    System.out.println("[INVALID INPUT, PLEASE INPUT P, C, OR S");
                                    continue;
                                }
                                break;
                            }
                            break;

                        case "C":
                            System.out.println("[OLD MAN] 'What do you mean? We've known each other for years, do you not remember me?' \n"
                                    + "[PLEASE ENTER EITHER YES (Y) OR NO (N)");
                            while (true) {
                                input = userInput.nextLine().toUpperCase();
                                switch (input) {
                                case "Y":
                                    System.out.println("Oh, I see... you play some very weird games, my friend. Hopefully, you remember the game I had for you though. You have to guess the number I'm thinking of right now. It's between 1 and 3, you get one try.");
                                    // Play the random number guessing game
                                    playRandomNumberGuesser(1, 3, userInput);
                                    break;
                                case "N":
                                    System.out.println("I get the feeling you aren't who you say you are. In order to validate your identity, I'm going to play a game with you. I'm thinking of a number between 1 and 3, what is it.");
                                    // Play the random number guessing game
                                    playRandomNumberGuesser(1, 3, userInput);
                                    break;
                                default:
                                    System.out.println("[INVALID INPUT, PLEASE INPUT Y, N]");
                                    continue;
                                }
                                break;
                            }
                            break;

                        case "S":
                            System.out.println("[OLD MAN] 'Ahhh not the talking type today, I see.'\n"
                                    + "[OLD MAN] If you do recall, I do have a game you have to play. It's a simple number guessing game. You guess a number between 1 and 3.");
                            // Play the random number guessing game
                            playRandomNumberGuesser(1, 3, userInput);
                            break;
                        default:
                            System.out.println("[INVALID INPUT, PLEASE ENTER Y OR N]");
                            continue;
                        }
                        break;
                    }
                    break;

                case "L":
                    System.out.println("You continue onto a path that leads to a tree \n"
                            + "The tree asks you to play a random number game where you pick a number between 1-3. \n");
                    playRandomNumberGuesser(1, 3, userInput);
                    break;
                    
                default:
                    System.out.println("[INVALID INPUT, PLEASE ENTER EITHER [R] OR [L]]");
                    continue;
                }
                break;
            }
// Mountainous area

            System.out.println("You continue on your journey and encounter a path that diverts left or right.");
            System.out.println("Which way do you go? [PLEASE ENTER R FOR RIGHT OR L FOR LEFT]");

            while (true) {
                input = userInput.nextLine().toUpperCase();
                switch (input) {
                    case "L":
                        System.out.println("You walk up a path, where the wind seems to get louder and louder and the air gets thinner and thinner.");
                        System.out.println("You suddenly hear a bark from a bush to your right. You open the bush to reveal a small puppy, it looks scared. Do you take it?");
                        System.out.println("[PLEASE INPUT EITHER YES (Y) OR NO (N)]");
                        
                        while (true) {
                            input = userInput.nextLine().toUpperCase();
                            switch (input) {
                                case "Y":
                                    System.out.println("You pick up the dog and fit him into your jacket, keeping him around for later");
                                    inventory.add("DOG");
                                    System.out.println("INVENTORY - " + inventory);
                                    System.out.println("You turn around and walk up the other part of the path into a forest");
                                    break;
                                case "N":
                                    System.out.println("You leave the dog alone.");
                                    System.out.println("You turn around and walk up the other part of the path into a forest");
                                    break;
                                default:
                                    System.out.println("[INVALID INPUT] Without enough time to react, the dog runs off.");
                                    System.out.println("You turn around and walk up the other part of the path into a forest");
                                    break;
                            }
                            break;
                        }

                    case "R":
                        System.out.println("You walk into a beautiful forest, almost mystical.");
                        System.out.println("There's a strong, quick gust of wind leading you to your right, almost pushing you towards it.");
                        System.out.println("Do you follow it?");
                        System.out.println("[PLEASE INPUT EITHER YES [Y] OR NO [N]");

                        // Prompt for user input
                        break;

                    default:
                        System.out.println("[INVALID INPUT, PLEASE ENTER [L] OR [R]");
                        break;
                }
                break;
            }
        }
    }

随机数生成游戏 -

    public static void playRandomNumberGuesser(int min, int max, Scanner userInput) {
        Random random = new Random();
        int randomNumber = random.nextInt((max - min) + 1) + min;
        int guess;
        boolean guessedCorrectly = false;

        do {
            System.out.print("Enter your guess (" + min + "-" + max + "): ");
            guess = userInput.nextInt();

            if (guess < min || guess > max) {
                System.out.println("Please enter a number between " + min + " and " + max + ".");
            } else if (guess < randomNumber) {
                System.out.println("Too low. Try again.");
            } else if (guess > randomNumber) {
                System.out.println("Too high. Try again.");
            } else if (guess == randomNumber) {
                System.out.println("[OLD MAN] 'Well done!! you've done well'\n"
                        + "[OLD MAN] 'You really do have a knack for these games. I'll tell you that. Here, take this for your journey.'\n");
                guessedCorrectly = true;
            }
        } while (!guessedCorrectly);
    }

输出的输出是不需要的。

输出-

一个老人从树上笑着出现 库存:[棍子] 你因你的伟大行为而被授予一根棍子。 你继续你的旅程,遇到一条向左或向右转移的路径。 你走哪条路?[请输入R表示右键或输入L表示左键] [输入无效,请输入[L]或[R]

期望输出 -

一个老人从树上笑着出现 库存:[棍子] 你因你的伟大行为而被授予一根棍子。 你继续你的旅程,遇到一条向左或向右转移的路径。 你走哪条路?[请输入R表示右键或输入L表示左键] (提示用户输入扫描仪以继续进入山区。

我尝试这样做,以便在第一个 while 循环结束时,它会提示用户玩游戏,然后进一步前进,但这只会导致错误,并没有真正做到我想要的。

我还尝试过将代码科学怪化到自身中,忽略类并将其放入代码中,但它没有这样做。它仍然显示问题。

谢谢大家。

java switch-statement do-while

评论

1赞 ViaTech 10/19/2023
您显示您想要和正在获得的输出是无法使用您当前的代码实现的......我对你在追求什么以及你的问题到底是什么有点困惑
0赞 Sup19 10/19/2023
从您提供的代码片段来看,尚不清楚您的代码块如何相互交互,以及您期望如何获得所需的输出。请详细说明问题并提供适当的代码片段。另外,您是否尝试过调试代码以检查实际痛点?
0赞 Basil Bourque 10/20/2023
en.wikipedia.org/wiki/Debugger

答: 暂无答案