为什么我的 Java Zork 克隆无法正确循环?

发布于 2024-11-02 03:28:12 字数 2221 浏览 1 评论 0原文

我正在制作 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();
        }
    }
}
}

I'm making a Zork clone, and whenever it loops, an error comes up in 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();
        }
    }
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故笙诉离歌 2024-11-09 03:28:12

运行代码到打开门的位置会

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

出现这种情况,因为您在最后调用了所有 third.close() 来关闭 System.in。只制作一台扫描仪,并且永远不要关闭它。

Running the code to the point of opening the door gives

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

This is happening because you are closing System.in with all your third.close() calls at the end. Make just one Scanner, and don't ever close it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文