NoSuchElementException运行Java代码时出错

时间:2019-10-08 20:18:45

标签: java nosuchelementexception

当我运行这段代码时,当我输入3时,它将引发奇怪的异常,否则在所有其他输入上,它将正常工作。当单独运行时,内部代码(如果输入== 3)可以正常运行。我不明白为什么它在运行时不起作用

我试图单独在错误块内运行代码,并且运行正常。

    public static void main(String[] args) {

        RiverCrossing game= new RiverCrossing();
        game.printWelcome();

        int tries=9;
        while(tries>0)
        {
            tries--;
            game.printNorthBank();
            game.printRiver();
            game.printSouthBank();

            int input = game.getInput();
            if(input==1)
            {
                if(game.getFarmerLocation())
                {
                    game.moveFarmertoSouth();
                    game.row2South(0);
                    System.out.println("Farmer moved to South");
                }
                else
                {
                    game.moveFarmertoNorth();
                    game.row2North(0);
                    System.out.println("Farmer moved to Northern Side");
                }
                boolean result=game.checkClashes();
                if(!result)
                {
                    break;
                }
            }
            else if(input==2)
            {
                if(game.getLocation(3)==false && game.getFarmerLocation()==false)
                {
                    game.row2North(3);
                    game.row2North(0);
                    game.moveFarmertoNorth();
                }
                else if(game.getLocation(3) && game.getFarmerLocation())
                {
                    game.row2South(3);
                    game.row2South(0);
                    game.moveFarmertoSouth();
                }
                else
                {
                    System.out.println("Cabbage not found on this side");
                }
                boolean result=game.checkClashes();
                if(!result)
                {
                    break;
                }
            }
            else if(input == 3)
            {
                if(game.getLocation(1) && game.getFarmerLocation())
                {
                    game.row2South(1);
                    game.row2South(0);
                    game.moveFarmertoSouth();
                }
                else if(game.getLocation(1)==false && game.getFarmerLocation()==false)
                {
                    game.row2North(1);
                    game.row2North(0);
                    game.moveFarmertoNorth();
                }
                else
                {
                    System.out.println("Cabbage not found on this side");
                }
                boolean result=game.checkClashes();
                if(!result)
                {
                    System.out.println("HERE5");
                    break;
                }
            }
            else if(input == 4)
            {
                if(game.getLocation(2)==false && game.getFarmerLocation()==false)
                {
                    game.row2North(2);
                    game.row2North(0);
                    game.moveFarmertoNorth();
                }
                else if(game.getLocation(2) && game.getFarmerLocation())
                {
                    game.row2South(2);
                    game.row2South(0);
                    game.moveFarmertoSouth();
                }
                else
                {
                    System.out.println("Wolf not found on this side");
                }
                boolean result=game.checkClashes();
                if(!result)
                {
                    break;
                }
            }
            else
            {
                tries=0;
            }   
        }        
    }
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 RiverCrossing.getInput(RiverCrossing.java:322)
    at RiverCrossing.main(RiverCrossing.java:345)

0 个答案:

没有答案