每当我运行此命令时,chooseCave()函数就可以与in.nextInt()一起正常工作。当我选择洞穴时,消息会每隔2秒弹出一次,然后当它越过该部分时,就会出现错误:

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at Dragon.main(Dragon.java:81)

我尝试了hasNextLine()hasNextInt(),当我在while hasNextLine()方法中使用main时,出现了很多错误。当我在while hasNextInt()方法中使用chooseCave()时,它不接受我的输入。

当我在if hasNextInt()方法中使用chooseCave()时,它不接受我的playAgain字符串输入,直接进入另一个游戏,但是hasNextInt() boolean 值返回false,并且它无限地向“Which Cave ...”发送垃圾邮件。

我已经阅读了错误报告,以及类似问题的Java文档和Stack Overflow。请帮忙。
import java.util.Scanner;
public class Dragon {

public static void displayIntro() {
    System.out.println("You are in a land full of dragons. In front of you, ");
    System.out.println("You see two caves. In one cave, the dragon is friendly");
    System.out.println("and will share his treasure with you. The other dragon");
    System.out.println("is greedy and hungry, and will eat you on sight");
    System.out.println(' ');
}

public static int chooseCave() {
    Scanner in = new Scanner(System.in);
    int cave = 0;
    while (cave != 1 && cave != 2) {
        System.out.println("Which cave will you go into? (1 or 2)");

        cave = in.nextInt();

    }
    in.close();
    return cave;
}

public static void checkCave(int chosenCave) {
    System.out.println("You approach the cave...");
    try
       {
       // Sleep at least n milliseconds.
       // 1 millisecond = 1/1000 of a second.
       Thread.sleep( 2000 );
       }
    catch ( InterruptedException e )
       {
       System.out.println( "awakened prematurely" );
       }
    System.out.println("It is dark and spooky...");
    try
       {
       // Sleep at least n milliseconds.
       // 1 millisecond = 1/1000 of a second.
       Thread.sleep( 2000 );
       }
    catch ( InterruptedException e )
       {
       System.out.println( "awakened prematurely" );
       }
    System.out.println("A large dragon jumps out in front of you! He opens his jaws and...");
    try
       {
       // Sleep at least n milliseconds.
       // 1 millisecond = 1/1000 of a second.
       Thread.sleep( 2000 );
       }
    catch ( InterruptedException e )
       {
       System.out.println( "awakened prematurely" );
       }

    double friendlyCave = Math.ceil(Math.random() * 2);

    if (chosenCave == friendlyCave) {
        System.out.println("Gives you his treasure!");
    }
    else {
        System.out.println("Gobbles you down in one bite!");
    }



}
public static void main(String[] args) {
    Scanner inner = new Scanner(System.in);
    String playAgain = "yes";
    boolean play = true;
    while (play) {
        displayIntro();
        int caveNumber = chooseCave();
        checkCave(caveNumber);
        System.out.println("Do you want to play again? (yes or no)");
        playAgain = inner.nextLine();
        if (playAgain == "yes") {
            play = true;
        }
        else {
            play = false;
        }
    }
    inner.close();

}

}

最佳答案

您关闭了第二个Scanner,后者又关闭了底层的InputStream,因此第一个Scanner无法再从相同的InputStream中读取,并且NoSuchElementException结果出现。

解决方案:对于控制台应用程序,请使用单个ScannerSystem.in中读取。

除了:如前所述,请注意Scanner#nextInt不使用换行符。在尝试使用nextLine再次调用Scanner#newLine()之前,请确保已消耗掉它们。

另请:Do not create multiple buffered wrappers on a single InputStream

10-07 16:16
查看更多