Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
IF语句完成后的Java refire循环_Java_Loops_User Input - Fatal编程技术网

IF语句完成后的Java refire循环

IF语句完成后的Java refire循环,java,loops,user-input,Java,Loops,User Input,我试图做一个简单的基于文本的冒险,作为对java初学者的练习,但我遇到了一个问题,经过长时间的搜索,我决定问一下。 我想知道在回答了IF语句后如何重新定义这个循环,例如用户输入:“Help”,显示“Help”选项卡,用户可以输入另一个命令。 我不知道为什么这不起作用,所以任何帮助都将不胜感激 boolean loopone = false; do { System.out.println(name + " is standing in a forest, covered in s

我试图做一个简单的基于文本的冒险,作为对java初学者的练习,但我遇到了一个问题,经过长时间的搜索,我决定问一下。 我想知道在回答了IF语句后如何重新定义这个循环,例如用户输入:“Help”,显示“Help”选项卡,用户可以输入另一个命令。 我不知道为什么这不起作用,所以任何帮助都将不胜感激

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

一个问题是,您需要这样做:

}while(loopone=false)

这:

}while(loopone==false)


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

感谢Blaine,我在每个if语句之后使用(loopone=false),我希望它现在返回顶部。工作正常。我看不到您在任何地方分配
loopone
,除了in
while(loopone=false)。这将始终返回true,并将创建一个无限循环。