簡體   English   中英

java.util.NoSuchElementException:找不到行。 在此之前有效,但以后不會生效。 我不知道為什么

[英]java.util.NoSuchElementException: No line found. Works before, but not later. I don't know why

我正在嘗試制作一款游戲,無論出於何種原因,Scanner之前都曾以一種單獨的方法工作,但並非以這種方法工作。

適用於:

Scanner in = new Scanner(System.in); // Scanner in case of invalid input

for (int i = 1; i <= p.getSpeed(); i++) { // loop to account for invalid direction

if (d.equalsIgnoreCase("right") || d.equalsIgnoreCase("r") && p.getX() <= width - 2) { // right

    if (board[p.getY()][p.getX() + 1] == ' ') {
        board[p.getY()][p.getX()] = ' ';
        p.setX(p.getX() + 1);
        board[p.getY()][p.getX()] = 'P';
    }

    else { // right not open
        System.out.println("Please input a valid direction");
        d = in.nextLine();
        i--;
    }

}

不適用於:

Scanner in = new Scanner(System.in); // Scanner in case of invalid input

int i = 1;

while (i == 1) {

    if (d.equalsIgnoreCase("right") || d.equalsIgnoreCase("r") && p.getX() <= width - 2) { // right

        for (int j = 0; j <= numZombies - 1; j++) {

            if (zombie[j].getX() == p.getX() + 1 && zombie[j].getY() == p.getY()) { // checks each zombie to see if it is one to the right of the player

                zombie[j].subtractHealth(p.getAttack());

                break;

            }

            if (zombie[j].getX() == p.getX() + 2 && zombie[j].getY() == p.getY()) {

                zombie[j].subtractHealth(p.getAttack());
                break;

            }else if (j == numZombies - 1) { // if no zombies are found

                System.out.println("There is no zombie there, please insert a new direction");
                d = in.nextLine();
                i--;

            }

        }

    }
}

所有的方法都可以工作並且邏輯很好,我只是不知道為什么它對第一個有效,而對第二個無效

關閉第一個掃描儀是問題所在,擺脫了固定的代碼。 謝謝查爾斯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM