使用扫描仪在Java中NoSuchElementException异常

时间:2019-01-31 20:15:08

标签: java

我正在构建一个程序,允许两名玩家玩 Tic-Tac-Tie 。我正在使用while循环使游戏继续进行,直到找到赢家为止,但是在第二次迭代中,我遇到了以下异常:

  

线程“ main”中的异常java.util.NoSuchElementException

Java程序

import java.util.Scanner;

public class Game {
public static void main(String[] args) {
    Board board1 = new Board();
    System.out.println("Game Starts:");
    board1.showBoard();
    while (board1.checkWin()!="X" && board1.checkWin()!="O") {
        System.out.print("What cell (0-8) do you want to play in:");
        Scanner sc = new Scanner(System.in);
        int i;
        i=sc.nextInt();
        System.out.print("What player are you:");
        Scanner sc1 = new Scanner(System.in);
        String s = sc1.nextLine();
        sc1.close();
        sc.close();
        char c=s.charAt(0);
        board1.makeMove(i, c);
        board1.showBoard();
        System.out.println();
        if(board1.checkWin()!="Draw") {
            System.out.print("Winner is " + (board1.checkWin()));
        }
    }
}
}

游戏开始:

. . .
. . .
. . .
What cell (0-8) do you want to play in:8
What player are you:X
. . .
. . .
. . X"

What cell (0-8) do you want to play in: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.main(Game.java:12)

2 个答案:

答案 0 :(得分:0)

您正在使用System.in初始化两个扫描仪。您可以在一个Scanner中实现所需的功能。如果您仍然想拥有两个Scanner对象(我看不到任何用例),则在创建第二个Scanner之前,先关闭它。

此外,这是在循环外打开或关闭资源的好方法。请参见下面的代码,其中在循环之外创建了Scanner对象。

    import java.util.Scanner;
    public class Game {

    public static void main(String[] args) {
    Board board1 = new Board();
    System.out.println("Game Starts:");
    board1.showBoard();
    Scanner sc = new Scanner(System.in);
    while (board1.checkWin()!="X" && board1.checkWin()!="O") {
        System.out.print("What cell (0-8) do you want to play in:");
        int i;
        i=sc.nextInt();
        sc.nextLine();// Reads the current line after number
        System.out.print("What player are you:");
        //Scanner sc1 = new Scanner(System.in); Not needed
        String s = sc.nextLine();
        //sc1.close();

        char c=s.charAt(0);
        board1.makeMove(i, c);
        board1.showBoard();
        System.out.println();
        if(board1.checkWin()!="Draw") {
            System.out.print("Winner is " + (board1.checkWin()));
        }
    }
    sc.close();
}
}

答案 1 :(得分:-1)

您不需要两个扫描仪对象即可获取输入。循环外的一个扫描器对象足以从键盘读取输入。它是一个缓冲区元素,它仅读取输入并将其分配给您希望输入的变量。

public class Game {
public static void main(String[] args) {
    Board board1 = new Board();
    System.out.println("Game Starts:");
    board1.showBoard();

    Scanner sc = new Scanner(System.in);

    while (board1.checkWin()!="X" && board1.checkWin()!="O") {
        System.out.print("What cell (0-8) do you want to play in:");
        int i;
        i=sc.nextInt();
        System.out.print("What player are you:");
        String s = sc.nextLine();
        char c=s.charAt(0);
        board1.makeMove(i, c);
        board1.showBoard();
        System.out.println();
        if(board1.checkWin()!="Draw") {
            System.out.print("Winner is " + (board1.checkWin()));
        }
    }
    sc.close();
}