IF语句完成后的Java重放循环

时间:2014-02-09 05:49:50

标签: java loops user-input

我正在尝试将一个简单的基于文本的冒险作为初学者java的练习,但我遇到了一个问题,经过长时间的搜索,我决定只是问它。 我想知道在IF语句被回答后如何重新启动这个循环,例如。用户输入:“帮助”,帮助标签显示,用户可以输入另一个命令。 我不知道为什么这不起作用,所以任何帮助都会非常感激。

boolean loopone = false;
    do {
    System.out.println(name + " is standing in a forest, covered in slime goo.");
    String cmdone = in.next();
    if (cmdone.equals("Help")) {
        System.out.println("---------Help---------");
        System.out.println("Type: [Help] to view help");
        System.out.println("Type: [Turn] to turn, syntax: [Turn (direction)] left, right and backward are the possible directions");
        System.out.println("Type: [Enter] to go through an entrance");
        System.out.println("Type: [Slay] to kill stuff");
        System.out.println("Type: [Take] to take stuff");
        System.out.println("Typing: [/rocket smallbrains] has no effect here");
        return;
        }
        else if (cmdone.equals("Enter")){
            System.out.println("Enter what?");  
            String conone = in.next();
            if (conone.equals("Forest")  || conone.equals("The forest")){
                System.out.println("You're already in the forest, dummy!");
            }
            else{
                System.out.println("I don't know where that is");
            }

        }
        else if (cmdone.equals("Turn right")){
            System.out.println("You turn right");
        }
    continue;
    }while (loopone = false);

1 个答案:

答案 0 :(得分:0)

你需要做一个问题:

}while (loopone = false);

这样:

}while (loopone == false);

否则,您只需要确保在每个loopone语句中将if设置为false。如果要退出,请将loopone设置为true(loopone = true)。