簡體   English   中英

什么是java.util.NoSuchElementException?如何解決?

[英]What is java.util.NoSuchElementException and how do i fix it?

我試圖編寫一個戰艦游戲,但在提示用戶輸入他們的猜測坐標時遇到了這個錯誤:

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 game.User.takeTurn(User.java:121)
        at game.Game.main(Game.java:61)

這是發生錯誤的方法User.takeTurn():

    static void takeTurn() {
           System.out.println("Yout turn!");
           System.out.println("Enter your x guess:");
           Scanner scanInput = new Scanner(System.in);
           int x;
           x = scanInput.nextInt();
           System.out.println("You entered "+ x + ", Now enter your y     coord:");
           int y;
           y = scanInput.nextInt();
           System.out.println("You entered + " + y);
           if(Board.aiBoard[x][y] == 2) {
               System.out.println("Hit! You got a hit at: " + x + "," + y);
               Board.enemyBoardDisplay[x][y] = 'h';
               Board.aiBoard[x][y] = 3;
           }
           else {
            System.out.println("Miss!");
            Board.enemyBoardDisplay[x][y] = 'x';
            Board.aiBoard[x][y] = 1;
          }

           scanInput.close();
      }

我不太確定這是否重要,但我在課堂上也使用了另一台掃描儀。 如果您需要更多代碼來幫助,請告訴我。 謝謝!

編輯1:好的,即使我做了scanInput.hasNextInt()之后,它也不起作用。 我還放入了else語句,該語句給出了xa值,但是現在x始終具有else語句給出的值。 它甚至要求輸入僅默認為該值的輸入。 但我不希望它成為默認值,我希望用戶選擇。

編輯2:這是我在主存根中調用代碼的地方

while (gameOn == true) {
            if (turn == 0) {
                User.takeTurn();
                Board.sunkCheck();
                if (gameOn == false)
                    break;
            }

            else if (turn == 1) {
                Computer.takeTurn();
                Board.sunkCheck();
                if (gameOn == false)
                    break;
            }

這是我在較早之前嘗試使用掃描儀的同一班級中使用掃描儀的地方:

System.out
                .println("\n Please enter the first x value for your ship  (Note, this will be the first point, from this point the \n"
                        + " ship can either go vertically downward or horizontally to the right so please choose the topmost point or leftmost \n"
                        + "point to do this. Also, the board is 10x10 but due to java array indexing the coordinates go x 0-9 y 0-9: ");
        try {
        Scanner coord = new Scanner(System.in);
        userX = coord.nextInt();
        System.out.println("You entered: " + userX);
        System.out.println("Now enter the y coordinate for this point: ");
        userY = coord.nextInt();
        System.out.println("You entered: " + userY);
        System.out
                .println("Please choose the direction of your ship. Enter v for verticle or h for horizontal(case sensitive): ");
        dir = (char) System.in.read();
        coord.close();
        }
        catch(InputMismatchException e) {
            System.out.println("Good job doof, since you tried to troll we did all the values for you!");
            userX = 3;
            userY = 3;
            dir = 'v';
        }

您可能需要檢查next元素,然后再使用scanInput.hasNextInt()將其放入Scanner


更新:

如果使用上面的Scanner coord = new Scanner(System.in); 然后使用Scanner scanInput = new Scanner(System.in);調用該函數Scanner scanInput = new Scanner(System.in); 而不關閉coord Scanner ,那么您將遇到問題。

掃描儀將繼續消耗數據流-這可能(將)導致意外的副作用。

您可能需要先關閉第一個,然后再使用另一個( 有關更多參考資料,請參見此處 )。


更新:

當您關閉coord.close()您正在關閉System.in並且當您嘗試從中重新讀取時,它將引發異常。

無需在每次需要輸入時重新打開流,而是只需創建一次Scanner即可重復使用它。

當您嘗試從掃描儀讀取輸入並且輸入不存在時,會發生NoSuchElementException

因此,在接受輸入之前,您應該檢查掃描儀是否有一些輸入供您使用,例如:

if (scanInput.hasNextInt()) {
    x = scanInput.nextInt();
}

暫無
暫無

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

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