繁体   English   中英

Try{} Catch {} 在 Java 中执行 catch before try{} [重复]

[英]Try{} Catch {} in Java is executing catch before try{} [duplicate]

我有一个我无法摆脱的问题。 我正在运行下面的代码并为此输入:

*输入单元格: XXOO_OX

| XX | | 面向对象 | | 牛 |

输入坐标:您应该输入数字! 输入坐标:一个 你应该输入数字: 输入坐标:ont 三 你应该输入数字! 输入坐标:1 3

| XXX | | 面向对象 | | 牛 |

进程以退出代码 0* 结束

在运行它之后,我在输入坐标之前收到了 catch 消息。 为什么? 我应该改变什么?

扫描仪扫描仪=新扫描仪(System.in); String[][] tictactoe = new String[3][3];

    //init method
    System.out.print("Enter cells: ");
    String s = scanner.next();


    String a = s.substring(0, 1);
    tictactoe[0][0] = a;
    String b = s.substring(1, 2);
    tictactoe[0][1] = b;
    String c = s.substring(2, 3);
    tictactoe[0][2] = c;
    String d = s.substring(3, 4);
    tictactoe[1][0] = d;
    String e = s.substring(4, 5);
    tictactoe[1][1] = e;
    String f = s.substring(5, 6);
    tictactoe[1][2] = f;
    String g = s.substring(6, 7);
    tictactoe[2][0] = g;
    String h = s.substring(7, 8);
    tictactoe[2][1] = h;
    String i = s.substring(8, 9);
    tictactoe[2][2] = i;

    for (int n = 0; n < 3; n++) {
        for (int m = 0; m < 3; m++) {
            String cuv = tictactoe[n][m];
            if (cuv.equals("_")) {
                tictactoe[n][m] =" ";
            }
        }
    }

            System.out.println("---------");
    System.out.println("| " + tictactoe[0][0] + " " + tictactoe[0][1] + " " + tictactoe[0][2] + " |");
    System.out.println("| " + tictactoe[1][0] + " " + tictactoe[1][1] + " " + tictactoe[1][2] + " |");
    System.out.println("| " + tictactoe[2][0] + " " + tictactoe[2][1] + " " + tictactoe[2][2] + " |");
    System.out.println("---------");

    String player1 = "X";
    String letter;
    boolean correctCoordinate=false;

    while (!correctCoordinate){
        System.out.print("Enter the coordinates:");

        String input=scanner.nextLine();
        String [] pieces = input.trim().split("\\s+");
        int x;
        int y;

        try {

            x = Integer.parseInt(pieces[0]);
            y = Integer.parseInt(pieces[1]);

            letter = tictactoe[3-y][x-1];

            if (letter.equals("X") || letter.equals("O")) {
                System.out.println("This cell is occupied! Choose another one!");
            } else {
                tictactoe[3-y][x-1]=player1;
                System.out.println("---------");
                System.out.println("| " + tictactoe[0][0] + " " + tictactoe[0][1] + " " + tictactoe[0][2] + " |");
                System.out.println("| " + tictactoe[1][0] + " " + tictactoe[1][1] + " " + tictactoe[1][2] + " |");
                System.out.println("| " + tictactoe[2][0] + " " + tictactoe[2][1] + " " + tictactoe[2][2] + " |");
                System.out.println("---------");
                correctCoordinate=true;
            }

        }catch (NumberFormatException err1) {
            System.out.println("You should enter numbers!");

        }catch (ArrayIndexOutOfBoundsException err2){
            System.out.println("Coordinates should be from 1 to 3!");
        }
    }

谢谢你,弗洛林

调试代码的最佳方法是堆栈跟踪。

尝试添加

       catch (NumberFormatException err1) {
            err1.printStackTrace();
            System.out.println("You should enter numbers!");

        }catch (ArrayIndexOutOfBoundsException err2){
            err2.printStackTrace();
            System.out.println("Coordinates should be from 1 to 3!");
        }

这样你就可以追踪你的问题。

希望对您有所帮助。

祝你今天过得愉快:)

暂无
暂无

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

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