0

私は 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)

これを理解するためにプログラムのコードがさらに必要かどうか教えてください。

4

1 に答える 1

0

最初に int があるかどうかを確認してください。交換

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

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

input.close();ここで、おそらく他の方法でも問題が発生すると思います。Scanner を閉じると、基になる Readable (この場合は System.in) が閉じられます (閉じた後は使用できません)。このプロジェクトでは、他の入力に System.in を使用する必要があるため、スキャナーを閉じないでください。

于 2015-01-30T23:23:16.190 に答える