Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.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
为什么我的Java Zork克隆不能正确循环?_Java_Eclipse_For Loop_Try Catch_Thread Sleep - Fatal编程技术网

为什么我的Java Zork克隆不能正确循环?

为什么我的Java Zork克隆不能正确循环?,java,eclipse,for-loop,try-catch,thread-sleep,Java,Eclipse,For Loop,Try Catch,Thread Sleep,我正在制作Zork克隆,每当它循环时,Eclipse中就会出现一个错误: import java.util.Scanner; public class Level1 { public static void main(String[] args) { int x; for(x=1; x<10; x++) { System.out.println ("Welcome to Sork - by Wyatt Lucas"); System.ou

我正在制作Zork克隆,每当它循环时,Eclipse中就会出现一个错误:

import java.util.Scanner;
public class Level1 {
public static void main(String[] args) {
    int x;  
    for(x=1; x<10; x++) {
        System.out.println ("Welcome to Sork - by Wyatt Lucas");
        System.out.println (" ");
        System.out.println ("Do you want to play?");
        Scanner first = new Scanner(System.in);
        @SuppressWarnings("unused")
        String firstInput;
        firstInput = first.nextLine();
        System.out.println("Well, it doesn't matter!");
        System.out.println("Use commands such as LOOK and GO NORTH \nto complete your adventure.");     
        System.out.println("");
        System.out.println("You are in a room.");
        Scanner second = new Scanner(System.in);
        String secondInput = second.nextLine();
        String look = "look";
        if(secondInput.equalsIgnoreCase(look)) {
            System.out.println("You look around and see a DOOR \nand a KEY on the floor.");
        }
        else {
            //don't use System.err.println. Just use System.out.println
            System.out.println("I do not understand that.");
            continue;
        }
        Scanner third = new Scanner(System.in);
        String thirdInput = third.nextLine();
        String pick_up_key = "pick up key";
        if(thirdInput.equalsIgnoreCase(pick_up_key)) {
            System.out.println("You picked up the KEY.");
        } else {
            System.out.println("I do not understand that.");
            continue;
        }
        Scanner fourth = new Scanner(System.in);
        String fourthInput = fourth.nextLine();
        String open_door = "open door";
        if(fourthInput.equalsIgnoreCase(open_door)) {
            System.out.println ("You open the door and are immediately \neaten by a grue!");
        } else {
            System.out.println("I do not understand that.");
        }

        first.close();
        second.close();
        third.close();
        fourth.close();
        try {
            Thread.sleep(1997);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
}
import java.util.Scanner;
公营一级{
公共静态void main(字符串[]args){
int x;

对于(x=1;x运行代码到打开车门时

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1516)
at Level1.main(Level1.java:12)

之所以发生这种情况,是因为您正在关闭System.in和所有
third.close()
最后调用。只做一个扫描器,永远不要关闭它。

您还没有报告问题所在。为什么要构造一个新的扫描器来读取每一行?您可以重复使用同一个扫描器。我可以说,这种方法不会扩大到完成整个游戏?或者如果是这样,它将是一个无法维护的代码库?