简体   繁体   English

尝试用Java制作TicTacToe程序时,我不断收到NoSuchElementException错误

[英]While trying to make a TicTacToe Program in Java, I keep getting a NoSuchElementException error

在此处输入图片说明 I have written some methods, and I keep getting a NoSuchElementException error when I try to run main, which allows me to play a Tic Tac Toe game. 我已经编写了一些方法,并且在尝试运行main时始终出现NoSuchElementException错误,这使我可以玩Tic Tac Toe游戏。 This occurs when I try to get a row and column value from the user, with enterMove(), as shown below: 当我尝试使用enterMove()从用户获取行和列值时,会发生这种情况,如下所示:

import java.util.Scanner;

public class TicTacToeMethods {

// Create a new board for players to use
public static String[][] createBoard(){

    String[][] board = new String[3][3];
    for(int r = 0; r < 3; r++){
        for(int c = 0; c < 3; c++){
            board[r][c] = " _ ";
        }
    }
    return board;

}

// Display the board each time a move is made
public static String displayBoard(String[][] board){

    String graphicalBoard = "";
    for(int r = 0; r < 3; r++){
        for(int c = 0; c < 3; c++){
            graphicalBoard += board[r][c];
        }
        graphicalBoard += "\n";
    }
    return graphicalBoard;

}

// Check to see if the game has been won
public static String gameWon(String[][] board){

    /*
    Board setup reference
    A B C
    D E F
    G E H

    A = (0,0) B = (0,1) C = (0,2)
    D = (1,0) E = (1,1) F = (1,2)
    G = (2,0) H = (2,1) I = (2,2)
    */
    String winner = "";
    boolean xWon = false;
    boolean oWon = false;

    // Check if X won
    boolean firstRowX = (board[0][0].equals(" X ") && board[0][1].equals(" X ") && board[0][2].equals(" X "));

    boolean secondRowX = (board[1][0].equals(" X ") && board[1][1].equals(" X ") && board[1][2].equals(" X "));

    boolean thirdRowX = (board[2][0].equals(" X ") && board[2][1].equals(" X ") && board[2][2].equals(" X "));

    boolean diagonalOneX = (board[0][0].equals(" X ") && board[1][1].equals(" X ") && board[2][2].equals(" X "));

    boolean diagonalTwoX = (board[0][2].equals(" X ") && board[1][1].equals(" X ") && board[2][0].equals(" X "));

    boolean[] resultsForX = {firstRowX,secondRowX,thirdRowX,diagonalOneX,diagonalTwoX};

    // Check if O won
    boolean firstRowO = (board[0][0].equals(" O ") && board[0][1].equals(" O ") && board[0][2].equals(" O "));

    boolean secondRowO = (board[1][0].equals(" O ") && board[1][1].equals(" O ") && board[1][2].equals(" O "));

    boolean thirdRowO = (board[2][0].equals(" O ") && board[2][1].equals(" O ") && board[2][2].equals(" O "));

    boolean diagonalOneO = (board[0][0].equals(" O ") && board[1][1].equals(" O ") && board[2][2].equals(" O "));

    boolean diagonalTwoO = (board[0][2].equals(" O ") && board[1][1].equals(" O ") && board[2][0].equals(" O "));

    boolean[] resultsForO = {firstRowO,secondRowO,thirdRowO,diagonalOneO,diagonalTwoO};

    for(boolean each : resultsForX){
        if(each == true){
            xWon = true;
            break;
        }
    }

    for(boolean each : resultsForO){
        if(each == true){
            oWon = true;
            break;
        }
    }

    // Return a winner, or a blank if no one has won
    if(xWon){
        winner = "X";
    }
    if(oWon){
        winner = "O";
    }
    return winner;

}

// Validate a player's move
public static boolean validMove(int row, int column, String[][] board){
    return ((board[row][column] != " X ") && (board[row][column] != " O "));
}

// Enter a move for the turn player
public static void enterMove(String turnPlayer,String[][] board){

    Scanner keyboard = new Scanner(System.in);
    //int row, column;
    boolean valid;

    System.out.print("Enter a row value for your move (1 - 3): ");
    **int row = keyboard.nextInt();**

    System.out.print("Enter a column value for your move (1 - 3): ");
    int column = keyboard.nextInt();

    valid = validMove(row - 1,column - 1,board);

    while(!valid){
        System.out.print("Enter a row value for your move (1 - 3): ");
        row = keyboard.nextInt();

        System.out.print("Enter a column value for your move (1 - 3): ");
        column = keyboard.nextInt();

        valid = validMove(row - 1,column - 1,board);
    }

    switch(turnPlayer){
        case "X":
            board[row - 1][column - 1] = " X ";
            break;
        case "O":
            board[row - 1][column - 1] = " O ";
            break;
        default:
            break;
    }

    keyboard.close();

}

} }

Is there something wrong with me allowing the user to choose from 1 - 3, instead of 0 - 2 for the array? 我有什么问题允许用户从1-3(而不是0-2)中选择数组吗? Or should I change something else? 还是应该更改其他内容? Thank you in advance! 先感谢您!

Note - The problematic line has been surrounded with asterisks, in enterMove() 注意-在EnterMove()中,有问题的行已用星号包围

EDIT: Stacktrace as requested 编辑:Stacktrace根据要求

You are closing the Scanner element, so you are closing the underlying InputStream , System.in 您正在关闭Scanner元素,因此正在关闭基础InputStreamSystem.in

So, no other Scanner can read again and a java.util.NoSuchElementException Will be thrown. 因此,其他扫描程序无法再次读取,并且将引发java.util.NoSuchElementException

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

相关问题 尝试创建循环时不断收到 java.util.NoSuchElementException 错误 - Keep getting java.util.NoSuchElementException error when trying to make a loop 继续在此程序上获取java.util.NoSuchElementException - Keep getting the java.util.NoSuchElementException on this program 为什么我不断收到nosuchelementexception错误? - Why do I keep getting the nosuchelementexception error? 在 Java 中使用 Scanner 继续获取 NoSuchElementException - Keep getting NoSuchElementException with Scanner in Java 我一直得到“线程中的异常”主“java.util.NoSuchElementException” - I keep getting “Exception in thread ”main“ java.util.NoSuchElementException” Java基本Tictactoe程序中的语义错误 - Semantic Error in basic Tictactoe program in Java 我在尝试创建 XML 文件时不断出错 - I keep getting error while trying to create an XML file 尝试在Java中建立计算器,但我不断收到.class错误 - Trying to build a calculator in java but I keep getting the .class error 我在尝试在 sts4 上发出获取请求时,在 postman 控制台上不断收到“错误:连接 ECONNREFUSED 192.168.0.29:8080” - I keep getting "Error: connect ECONNREFUSED 192.168.0.29:8080 " On postman console while im trying to make a get request on sts4 不知道为什么我收到java.util.NoSuchElementException错误 - Not sure why i'm getting an java.util.NoSuchElementException error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM