【问题标题】:Java Tic Tac Toe gameJava井字游戏
【发布时间】:2020-03-21 15:43:44
【问题描述】:

我是一名初学者,正在学习 Java 的超技能课程。我试图在 VS Code 中尝试 this tic tac toe 游戏项目。它工作得很好。但是代码在提交时出错了。

代码:

package tictactoe;
import java.util.*;

public class Main {
    public static int movesCommitted = 0;
    static String[][][] matrix = {{{" ", "13"}, {" ", "23"}, {" ", "33"}}, 
                                  {{" ", "12"}, {" ", "22"}, {" ", "32"}},
                                  {{" ", "11"}, {" ", "21"}, {" ", "31"}}};

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean drawCounter = true;
        board(matrix);
        do {
            String symbol = (movesCommitted % 2 == 0) ? "X" : "O";
            validateAndAppend(symbol, scanner);     // Validate for wrong input and filled spots
            board(matrix);    // Display board
            movesCommitted++;
            if (anyWinner(symbol)) {
                drawCounter = false;
                System.out.println(symbol + " wins");
                break;
            }

        } while (movesCommitted < 9);

        if (drawCounter) {
            System.out.println("Draw");
        }
    }

    public static void board(String[][][] arr) {
        System.out.println("---------");
        System.out.println("| " + arr[0][0][0] + " " + arr[0][1][0] + " " + arr[0][2][0] + " |");
        System.out.println("| " + arr[1][0][0] + " " + arr[1][1][0] + " " + arr[1][2][0] + " |");
        System.out.println("| " + arr[2][0][0] + " " + arr[2][1][0] + " " + arr[2][2][0] + " |");
        System.out.println("---------");
    }

    public static void validateAndAppend(String symbol, Scanner scanner) {
        boolean cont = true;
        do {
            System.out.print("Enter coordinates: ");
            String inp = scanner.nextLine();  // Input move
            int dataInput;
            String input = "0" + inp;
            input = input.replace(" ", "");
            if (Integer.parseInt(input) < 11) {
                System.out.println("You should enter numbers!");
                continue;
            } else {
                dataInput = Integer.parseInt(input);
            }

            int unit = dataInput % 10;
            int tens = dataInput / 10;

            if (unit < 1 || unit > 3 || tens < 1 || tens > 3) {
                System.out.println("Coordinates should be from 1 to 3!");
                continue;
                // break;
            }

            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    if (matrix[i][j][1].toString().equals(String.valueOf(dataInput))) {
                        if (matrix[i][j][0] == " ") {
                            matrix[i][j][0] = symbol;
                            cont = false;
                        } else {
                            System.out.println("This cell is occupied! Choose another one!");
                        }
                    }
                }
            }
        } while (cont);
    }

    public static Boolean anyWinner(String xo) {
        if (movesCommitted < 5) {
            return false;
        }

        for (int i = 0; i <= 2; i++) {
            if (matrix[i][0][0] == xo && matrix[i][1][0] == xo && matrix[i][2][0] == xo) {
                return true;    // This is row check
            }

            if (matrix[0][i][0] == xo && matrix[1][i][0] == xo && matrix[2][i][0] == xo) {
                return true;    // This is column check
            }
        }

        if (matrix[0][0][0] == xo && matrix[1][1][0] == xo && matrix[2][2][0] == xo) {
            return true;    // This is "\" diagonal check
        }

        if (matrix[0][2][0] == xo && matrix[1][1][0] == xo && matrix[2][0][0] == xo) {
            return true;    // This is "/" check
        }

        return false;
    }
}

错误:

Exception in test #2

Probably your program run out of input (Scanner tried to read more than expected). If you are sure it's not, this type of exception also happens if you created more than one Scanner object (it is preferred to use a single Scanner in program).

java.util.NoSuchElementException: No line found
    at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
    at tictactoe.Main.validateAndAppend(Main.java:44)
    at tictactoe.Main.main(Main.java:16)

Please find below the output of your program during this failed test.
Note that the '>' character indicates the beginning of the input line.

---

---------
|       |
|       |
|       |
---------
Enter coordinates: >4 1
Coordinates should be from 1 to 3!
Enter coordinates: >3 1
This cell is occupied! Choose another one!
Enter coordinates: >4 4
Coordinates should be from 1 to 3!
Enter coordinates: >2 2
This cell is occupied! Choose another one!

原始错误很长(输入)。似乎错误出在 validateAndAppend() 方法中,程序无法区分站点上的错误输入和正确输入。

【问题讨论】:

    标签: java


    【解决方案1】:

    我认为网站的输入测试导致扫描String inp 出现问题。

    你得到的错误 (java.util.NoSuchElementException: No line found) 是由于扫描仪没有找到下一行,所以在将输入添加到 inp 之前尝试检查是否存在任何行:

    System.out.print("Enter coordinates: ");
    
    String inp="$";
    
    if(scanner.hasNextLine())    // Check if next line exists
        inp=scanner.nextLine();  // Input move
    
    if(inp.equals("$"))   // If inp did not change show an error and input again
    {
        System.out.println("Input error");
        continue;
    }
    

    编辑

    这是另一种可以帮助您的方法:

    System.out.print("Enter coordinates: ");
    
    String inp=null;
    try
    {
        inp=scanner.nextLine();
    } catch(Exception e)
    {
        return;
    }
    
    int dataInput;
    // rest of you code
    

    【讨论】:

    • 我明白,但程序现在执行无限循环并且构建失败
    • 好的,如果在String inp=scanner.nextLine(); 之前添加if(!scanner.hasNextLine()) return; 会怎样?另外请删除我之前的回答所做的任何更改。只需将该行添加到您的原始代码中即可。
    • 错误依旧。唯一的区别是代码在最后没有输入的情况下工作
    • @AnikinSkywalker 很难调试,因为除了提交测试之外,程序运行良好,但我编辑了答案并添加了另一种可以解决问题的方法。
    • 我猜是他们的系统出了问题。异常处理不起作用。感谢您的努力
    猜你喜欢
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    相关资源
    最近更新 更多