扫描程序出现nosuchelementexception错误

时间:2015-01-30 23:09:21

标签: java nosuchelementexception

我对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)

请告诉我你是否需要更多的程序代码来解决这个问题。

1 个答案:

答案 0 :(得分:0)

首先检查是否有任何int。取代

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

使用

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

我认为你的问题出现了,因为你在这里input.close();,也可能在其他方法中。如果你关闭Scanner它会关闭底层的Readable,在这种情况下是System.in(关闭后你不能使用它)。在此项目中,您希望将System.in用于其他输入,因此请勿关闭扫描程序