为什么我不能让这个reoccuring循环工作?

时间:2017-03-14 22:40:01

标签: java

以下是我正在处理的问题的链接:http://programmingbydoing.com/a/adventure2.html

我似乎无法到达楼下区域。我们应该主要使用while和if循环,所以如果有可能从循环的一个区域到另一个区域,那么听到输入会很棒!

public class Adventure2 {

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        int nextroom = 1;
        String choice = "";

        System.out.println("MITCHELL'S TINY ADVENTURE 2!");
        System.out.println("")

        while (nextroom != 0) {
            if (nextroom == 1) {
                System.out.println("You are in a creepy house! Would you like to go to the \"kitchen\" or \"upstairs\"?");
                System.out.print("> ");
                choice = keyboard.next();

                if (nextroom == 1) {
                    System.out.println("There is a long countertop with dirty dishes everywhere.  Off to one side there is, as you'd expect, a refrigerator. You may open the \"refrigerator\" or go \"back\".");
                    choice = keyboard.next();
                    if (choice.equals("back")) {
                        System.out.println("You are in a creepy house! Would you like to go to the \"kitchen\" or \"upstairs\"?");
                        System.out.println("> ");
                        choice = keyboard.next();
                    } else {
                        System.out.println("The refrigerator falls on you.");

                        if (nextroom == 2) {
                            System.out.println("You are in a creepy house! Would you like to go to the \"kitchen\" or \"upstairs\"?");
                            System.out.println("> ");
                            choice = keyboard.next();
                            if (choice.equals("upstairs")) {
                                System.out.println("Upstairs you see a hallway.  At the end of the hallway is the master \"bedroom\".  There is also a \"bathroom\" off the hallway. Or you can go back \"downstairs\". Where would you like to go?");
                                System.out.println("> ");
                                choice = keyboard.next();
                                if (choice.equals("downstairs")) {
                                    System.out.println("You are in a creepy house! Would you like to go to the \"kitchen\" or \"upstairs\"?");

                                }

                            }
                        }
                    }
                }
            }
       }
    }
}

1 个答案:

答案 0 :(得分:1)

坦率地说,你的代码有点混乱,但这对初学者来说并不罕见。

诀窍是仔细阅读您编写的代码,并“在脑中运行它”。在您的示例中,您将会到达这一点:

        System.out.println("You are in a creepy house!...");
        System.out.print("> ");
        choice = keyboard.next();

你读了一个选择。但接下来会发生什么?你怎么用这个选择?答案......没事!以下是您接下来要做的事情。

        if (nextroom == 1) {
            System.out.println("There is a long countertop ...;
            choice = keyboard.next();

您忽略了您阅读的第一个choice,再次测试nextroom(由于您没有为其分配任何新内容,因此不会更改。

如果你仔细阅读你的代码并尝试在头脑中运行它(就像计算机一样),那么这些问题应该跳出来。如果这太难了,那么使用调试器会有所帮助。

这让我们回到原来的评论。当你开始了解应该正在发生什么时,你应该意识到你的程序应遵循的一般模式。像这样的东西

 while (not finished):
     print description of where I am
     print choices of where to go next next
     get choice
     if choice is valid:
         change location
     else:
         print error message

如果你做得对,你不需要多次在循环体中复制和粘贴图案。

  • 提示:考虑使用数组和数组索引。
  • 提示2:nextroom可用作数组索引。