扫描仪出现 nosuchelementexception 错误

nosuchelementexception error with scanner

我对 java 编程还很陌生,我在尝试编写游戏时遇到了一些困难。以下是导致错误的方法。

public static void dragonNest(Dragon player) {
    int loop = 0;
    Scanner input = new Scanner(System.in);
    Territory dragonLand = new Territory(10, 5);
    loop = dragonLand.population + loop;
    int menuChoice;
    int ventureChoice;
    boolean choice = false;
    boolean validOption = true;
    System.out.println("Welcome to your nest, " + player.dragonName);
    while (choice == false) {
        System.out.println("What would you like to do?");
        System.out.println("Press 1 to view your hoard");
        System.out.println("Press 2 to venture forth from your nest");
        menuChoice = input.nextInt();
        if (input.hasNextInt()) {

            if (menuChoice > 5 || menuChoice < 1) {
                System.out.println("That is not a valid option");;
            }
            else if (menuChoice == 1){
                System.out.println("Your hoard is worth " + player.dragonHoard + " gold pieces.");
            }
            else if (menuChoice == 2) {
                System.out.println("You fly forth from your den");
                System.out.println("Press 1 to raid a nearby village, 2 to expand your territory, and 3  to return to your nest");
                ventureChoice = input.nextInt();
                do  {
                    if (ventureChoice < 3 || ventureChoice < 1) {
                        System.out.println("That is not a valid option");
                        validOption = false; 
                    }
                    else if (ventureChoice == 1) {
                        System.out.println("You fly forth to raid a nearby village");
                        Random generator = new Random();
                        int dragonPower = player.dragonLevel * 2;
                        int villageOffense = generator.nextInt() + 20 + dragonPower;
                        int villageDefense = generator.nextInt() + 20 + dragonPower;
                        int villageFirepower = generator.nextInt() + 20 + dragonPower;
                        int villageWealth = generator.nextInt() + 20 + dragonPower;
                        Village enemy = new Village(villageOffense, villageDefense, villageFirepower, villageWealth);
                        player = villageCombat(player, enemy);
                    }
                }
                while (validOption == false);
            }
            input.close();
        }
    }

当我运行程序时,这是显示的文本:

What will be the name of your dragon?
Example
Select your dragon category
Press 1 for a metallic dragon, 2 for a chromatic dragon, and 3 for a primal dragon
1
Are you a brass, copper, bronze, silver or gold dragon?
Press 1 for brass, 2 for copper, 3 for bronze, 4 for silver, and 5 for gold.
1
Welcome to your nest, Example
What would you like to do?
Press 1 to view your hoard
Press 2 to venture forth from your nest
Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at DragonNest.dragonNest(DragonNest.java:141)
    at DragonNest.dragonGeneration(DragonNest.java:125)
    at DragonNest.main(DragonNest.java:9)

如果您需要更多程序代码来解决这个问题,请告诉我。

先看看有没有int就可以了。替换

 menuChoice = input.nextInt();
 if (input.hasNextInt()) {...

 if (input.hasNextInt()) {
       menuChoice = input.nextInt();
       ...

我认为你的问题出现是因为你在这里有 input.close(); 并且可能在其他方法中也有。如果关闭 Scanner,它会关闭底层的 Readable,在本例中为 System.in(关闭后无法使用它)。在这个项目中,你想使用 System.in 作为一些其他输入,所以不要关闭 Scanner