英文:
Try{} Catch {} in Java is executing catch before try{}
问题
我遇到一个问题,无法摆脱它。
我正在运行以下代码,对于这个输入:
*输入单元格:XXOO_OX
| X X |
| O O |
| O X |
输入坐标:您应该输入数字!
输入坐标:one
您应该输入数字!
输入坐标:ont three
您应该输入数字!
输入坐标:1 3
| X X X |
| O O |
| O X |
进程退出,退出代码为0*
运行后,我在输入坐标之前捕获到异常消息。为什么?我应该更改什么?
Scanner scanner = new Scanner(System.in);
String[][] tictactoe = new String[3][3];
// 初始化方法
System.out.print("输入单元格:");
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] + " |");
String player1 = "X";
String letter;
boolean correctCoordinate = false;
while (!correctCoordinate) {
System.out.print("输入坐标:");
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("该单元格已被占用!请选择其他单元格!");
} 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("您应该输入数字!");
} catch (ArrayIndexOutOfBoundsException err2) {
System.out.println("坐标应该在1到3之间!");
}
}
谢谢,
Florin
英文:
I am having an issue that I cannot get rid off.
I am running the code from bellow and for this input:
*Enter cells: XXOO_OX
| X X |
| O O |
| O X |
Enter the coordinates:You should enter numbers!
Enter the coordinates:one
You should enter numbers!
Enter the coordinates:ont three
You should enter numbers!
Enter the coordinates:1 3
| X X X |
| O O |
| O X |
Process finished with exit code 0*
and after running it I get the catch message before I input the coordinates. Why? What should I change?
Scanner scanner = new Scanner(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!");
}
}
Thank you,
Florin
答案1
得分: 1
调试代码的最佳方法是使用堆栈跟踪。
尝试添加以下代码块:
catch (NumberFormatException err1) {
err1.printStackTrace();
System.out.println("您应该输入数字!");
} catch (ArrayIndexOutOfBoundsException err2) {
err2.printStackTrace();
System.out.println("坐标应该在1到3之间!");
}
这样你可以追踪你的问题。
希望这对你有帮助。
祝你有一个愉快的一天
英文:
The best way to debug your code is by stack tracing.
Try adding
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!");
}
This way you can trace your issue.
Hope it was helpful.
Have a nice day
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论