扫描仪只接受一次输入

时间:2013-08-15 16:19:46

标签: java

最近,我一直在研究我的Noughts and Crosses游戏,但我偶然发现了一个我似乎无法解决的问题。

每当我第一次输入内容时,它都能正常工作并打印出电路板,但是,从那时起,它就会停止检查并打印出电路板。

        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!");
                        }
                    }
                }
            }
        }
    }

这是印刷电路板的方法:

    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;
                }
            }
        }
    }
}

另外,我知道这个代码是一个很大的混乱,并且有很多其他更有效的方法可以做到这一点,但由于我是一个相当新的程序员,我选择使用功能而不是效率

1 个答案:

答案 0 :(得分:4)

您的else if(playerTurn == 2)行需要提取到与if(playerTurn == 1)相同的水平:

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

应该成为

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