简体   繁体   中英

Scanner only accepting input once

Recently, I've been working on my Noughts and Crosses game, but I've stumbled upon a problem that I can't seem to be able to fix.

Whenever I type in something the first time, it works and prints out the board, however, from that point onward, it just stops checking and printing out the board.

        while(inf){
        String temp = input.next();
        if(playerTurn == 1){
            if(amountP1 <= 3){
                if(temp.equalsIgnoreCase("00")){
                    if(board[0][0] == 0){
                        board[0][0] = 1;
                        showBoard(board);
                        playerTurn = 2;
                        amountP1++;
                    }
                    else{
                        System.out.println("This space is already occupied!");
                    }
                }
                else if(temp.equalsIgnoreCase("01")){
                    if(board[0][1] == 0){
                        board[0][1] = 1;
                        showBoard(board);
                        playerTurn = 2;
                        amountP1++;
                    }
                    else{
                        System.out.println("This space is already occupied!");
                    }
                }
                else if(temp.equalsIgnoreCase("02")){
                    if(board[0][2] == 0){
                        board[0][2] = 1;
                        showBoard(board);
                        playerTurn = 2;
                        amountP1++;
                    }
                    else{
                        System.out.println("This space is already occupied!");
                    }
                }
                else if(temp.equalsIgnoreCase("10")){
                    if(board[1][0] == 0){
                        board[1][0] = 1;
                        showBoard(board);
                        playerTurn = 2;
                        amountP1++;
                    }
                    else{
                        System.out.println("This space is already occupied!");
                    }
                }
                else if(temp.equalsIgnoreCase("11")){
                    if(board[1][1] == 0){
                        board[1][1] = 1;
                        showBoard(board);
                        playerTurn = 2;
                        amountP1++;
                    }
                    else{
                        System.out.println("This space is already occupied!");
                    }
                }
                else if(temp.equalsIgnoreCase("12")){
                    if(board[1][2] == 0){
                        board[1][2] = 1;
                        showBoard(board);
                        playerTurn = 2;
                        amountP1++;
                    }
                    else{
                        System.out.println("This space is already occupied!");
                    }
                }
                else if(temp.equalsIgnoreCase("20")){
                    if(board[2][0] == 0){
                        board[2][0] = 1;
                        showBoard(board);
                        playerTurn = 2;
                        amountP1++;
                    }
                    else{
                        System.out.println("This space is already occupied!");
                    }
                }
                else if(temp.equalsIgnoreCase("21")){
                    if(board[2][1] == 0){
                        board[2][1] = 1;
                        showBoard(board);
                        playerTurn = 2;
                        amountP1++;
                    }
                    else{
                        System.out.println("This space is already occupied!");
                    }
                }
                else if(temp.equalsIgnoreCase("22")){
                    if(board[2][2] == 0){
                        board[2][2] = 1;
                        showBoard(board);
                        playerTurn = 2;
                        amountP1++;
                    }
                    else{
                        System.out.println("This space is already occupied!");
                    }
                }
            }
            else if(playerTurn == 2){
                if(amountP2 <= 3){
                    if(temp.equalsIgnoreCase("00")){
                        if(board[0][0] == 0){
                            board[0][0] = 2;
                            showBoard(board);
                            playerTurn = 1;
                            amountP2++;
                        }
                        else{
                            System.out.println("This space is already occupied!");
                        }
                    }
                    else if(temp.equalsIgnoreCase("01")){
                        if(board[0][1] == 0){
                            board[0][1] = 2;
                            showBoard(board);
                            playerTurn = 1;
                            amountP2++;
                        }
                        else{
                            System.out.println("This space is already occupied!");
                        }
                    }
                    else if(temp.equalsIgnoreCase("02")){
                        if(board[0][2] == 0){
                            board[0][2] = 2;
                            showBoard(board);
                            playerTurn = 1;
                            amountP2++;
                        }
                        else{
                            System.out.println("This space is already occupied!");
                        }
                    }
                    else if(temp.equalsIgnoreCase("10")){
                        if(board[1][0] == 0){
                            board[1][0] = 2;
                            showBoard(board);
                            playerTurn = 1;
                            amountP2++;
                        }
                        else{
                            System.out.println("This space is already occupied!");
                        }
                    }
                    else if(temp.equalsIgnoreCase("11")){
                        if(board[1][1] == 0){
                            board[1][1] = 2;
                            showBoard(board);
                            playerTurn = 1;
                            amountP2++;
                        }
                        else{
                            System.out.println("This space is already occupied!");
                        }
                    }
                    else if(temp.equalsIgnoreCase("12")){
                        if(board[1][2] == 0){
                            board[1][2] = 2;
                            showBoard(board);
                            playerTurn = 1;
                            amountP2++;
                        }
                        else{
                            System.out.println("This space is already occupied!");
                        }
                    }
                    else if(temp.equalsIgnoreCase("20")){
                        if(board[2][0] == 0){
                            board[2][0] = 2;
                            showBoard(board);
                            playerTurn = 1;
                            amountP2++;
                        }
                        else{
                            System.out.println("This space is already occupied!");
                        }
                    }
                    else if(temp.equalsIgnoreCase("21")){
                        if(board[2][1] == 0){
                            board[2][1] = 2;
                            showBoard(board);
                            playerTurn = 1;
                            amountP2++;
                        }
                        else{
                            System.out.println("This space is already occupied!");
                        }
                    }
                    else if(temp.equalsIgnoreCase("22")){
                        if(board[2][2] == 0){
                            board[2][2] = 2;
                            showBoard(board);
                            playerTurn = 1;
                            amountP2++;
                        }
                        else{
                            System.out.println("This space is already occupied!");
                        }
                    }
                }
            }
        }
    }

And this is the method for printing out the board:

    public static void showBoard(int x[][]){
    int rowNumber = 1;
    for(int row = 0 ; row < x.length ; row++){
        for(int column = 0 ; column < x[row].length ; column++){
            if(x[row][column] == 0){
                if(rowNumber <= 2){
                System.out.print(" E");
                rowNumber++;
                }
                else if(rowNumber == 3){
                    System.out.println(" E");
                    rowNumber = 1;
                }
            }
            else if(x[row][column] == 1){
                if(rowNumber <= 2){
                System.out.print(" X");
                rowNumber++;
                }
                else if(rowNumber == 3){
                    System.out.println(" X");
                    rowNumber = 1;
                }
            }
            else if(x[row][column] == 2){
                if(rowNumber <= 2){
                System.out.print(" O");
                rowNumber++;
                }
                else if(rowNumber == 3){
                    System.out.println(" O");
                    rowNumber = 1;
                }
            }
        }
    }
}

Also, I am aware that this code is a big mess and there's problably a ton of other, more efficient ways to do this, but since I am a fairly new programmer, I chose to go with functionality over efficiency with this one.

Your else if(playerTurn == 2) line needs to be pulled out to the same level as if(playerTurn == 1) :

if(playerTurn == 1){
    if(amountP1 <= 3){
        //...
    }
    else if(playerTurn == 2){
        //...
    }
}

should become

if(playerTurn == 1){
    if(amountP1 <= 3){
        //...
    }
} else if(playerTurn == 2) {
    //...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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