简体   繁体   English

扫描仪无此元素异常

[英]Scanner no such element exception

got this code: 得到此代码:

private void runGame() {
    boolean won = false;

    while(won == false) {
        System.out.println("DEBUG A");
        Player p = this.nextPlayer();
        System.out.println("DEBUG B");
        Scanner s = new Scanner(System.in);
        if(p.getPlayerType() == 0) {
            System.out.println("DEBUG C");
            // is Human
            BoardPrinter.printBoard(this.b);

            // Choose figure
            BoardPrinter.printFigureChoose(this.b.getFiguresFromPlayer(p.getSymbol()),p);

            int id = this.checkInput(1,this.b.getFiguresFromPlayer(p.getSymbol()).size(),s.nextInt());
            Figure f = this.b.getFiguresFromPlayer(p.getSymbol()).get(id - 1);

            // Choose move              
            ArrayList<Move> moves = b.checkMoves(f);
            BoardPrinter.printMoveChoose(moves,p);
            int moveID = this.checkInput(1,moves.size(),s.nextInt());
            Move m = moves.get(moveID - 1);

            // Execute the chosen move
            this.b.getCellAt(f.getLocation().getX(), f.getLocation().getY()).setSymbol(Symbol.EMPTY);
            f.setLocation(m.getTarget());
            this.b.getCellAt(m.getTarget().getX(), m.getTarget().getY()).setSymbol(p.getSymbol());

            // Check win conditions
            won = this.checkVictory(p);
            System.out.println("DEBUG D");
        }
        else {
            // KI stuff

            KI k = new KI(this.b,p,p.getPlayerType());
            Move m = k.think();     

            // Execute chosen move
            Figure f = this.b.getFigureAt(m.getStart());
            this.b.getCellAt(m.getStart().getX(), m.getStart().getY()).setSymbol(Symbol.EMPTY);
            f.setLocation(m.getTarget());
            this.b.getCellAt(m.getTarget().getX(), m.getTarget().getY()).setSymbol(p.getSymbol());
        }

        // Decomment this to get debug info
        //DebugPrinter.print(this.b);

        s.close();
    }
}

At the moment i do get an java.util.NoSuchElementException. 目前,我确实获得了java.util.NoSuchElementException。 Yes i know what i means and i googled a lot. 是的,我知道我的意思,而且我在Google上搜索了很多。 Still i don't understand why it get's thrown. 仍然我不明白为什么它被抛出。 Also, i do get my Debug output only until "C". 另外,我的确仅在“ C”之前获得调试输出。

If i move the Scanner initialization outside of the while loop i don't get that Exception anymore. 如果我将Scanner初始化移到while循环之外,则不会再收到该异常。 However i do get stuck to, with the Code stopping at Debug "A" and nothing else. 但是,我确实陷入困境,代码在调试“ A”处停止,仅此而已。

I've encountered this both when having 2 human players. 当有2位人类玩家时,我都遇到了这两种情况。 Fun fact: The first player will always work perfectly. 有趣的事实:第一个玩家将始终表现完美。 The Player can see the Board, choose a Figure, choose a move and execute it. 玩家可以看到棋盘,选择一个人物,选择一个动作并执行它。 Whenever the next while() run starts and the 2nd player comes into account it fails as i described it above depended on where my Scanner initialization is. 每当下一次while()运行开始并且考虑到第二个播放器时,它都会失败,因为如上所述,这取决于我的Scanner初始化位置。

Any guesses what's the problem? 有什么问题吗? Thanks a lot! 非常感谢! :) :)

You close your scanner at the end of your while loop. 您在while循环结束时关闭扫描仪。 This means that the next time you try to read something from the scanner you'll get a NoSuchElementException . 这意味着下次您尝试从扫描仪读取某些内容时,将收到NoSuchElementException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM