Try{}Java中的Catch{}在Try{}之前执行Catch
我有一个无法摆脱的问题。 我正在运行下面的代码,对于此输入: *输入单元格:XXOO\u OX |X X| |O O| |氧| 输入坐标:您应该输入数字强> 输入坐标:1 你应该输入数字! 输入坐标:ont 3 你应该输入数字! 输入坐标:1 3 |X X X| |O O| |氧| 进程已完成,退出代码为0* 运行它之后,在输入坐标之前,我会得到catch消息。为什么?我应该换什么 扫描仪=新的扫描仪(System.in); 字符串[][]tictactoe=新字符串[3][3]Try{}Java中的Catch{}在Try{}之前执行Catch,java,try-catch,Java,Try Catch,我有一个无法摆脱的问题。 我正在运行下面的代码,对于此输入: *输入单元格:XXOO\u OX |X X| |O O| |氧| 输入坐标:您应该输入数字 输入坐标:1 你应该输入数字! 输入坐标:ont 3 你应该输入数字! 输入坐标:1 3 |X X X| |O O| |氧| 进程已完成,退出代码为0* 运行它之后,在输入坐标之前,我会得到catch消息。为什么?我应该换什么 扫描仪=新的扫描仪(System.in); 字符串[][]tictactoe=新字符串[3][3] //ini
//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!");
}
}
//初始化方法
系统输出打印(“输入单元格:”;
字符串s=scanner.next();
字符串a=s.substring(0,1);
tictactoe[0][0]=a;
字符串b=s.子字符串(1,2);
tictactoe[0][1]=b;
字符串c=s.substring(2,3);
tictactoe[0][2]=c;
字符串d=s.substring(3,4);
tictactoe[1][0]=d;
字符串e=s.子字符串(4,5);
tictactoe[1][1]=e;
字符串f=s.子字符串(5,6);
tictactoe[1][2]=f;
字符串g=s.substring(6,7);
tictactoe[2][0]=g;
字符串h=s.子字符串(7,8);
tictactoe[2][1]=h;
字符串i=s.子字符串(8,9);
tictactoe[2][2]=i;
对于(int n=0;n<3;n++){
对于(int m=0;m<3;m++){
字符串cuv=tictactoe[n][m];
if(cuv.等于(“”)){
提克塔克托[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(“-----------”);
字符串播放器1=“X”;
字符串字母;
布尔坐标=假;
而(!correctCoordinate){
System.out.print(“输入坐标:”);
字符串输入=scanner.nextLine();
String[]pieces=input.trim().split(\\s+);
int x;
int-y;
试一试{
x=整数.parseInt(片段[0]);
y=整数.parseInt(片段[1]);
字母=tictactoe[3-y][x-1];
如果(字母等于(“X”)|字母等于(“O”)){
System.out.println(“此单元格已被占用!请选择另一个!”);
}否则{
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(“-----------”);
校正坐标=真;
}
}捕获(NumberFormatException错误1){
System.out.println(“您应该输入数字!”);
}捕获(ArrayIndexOutOfBoundsException错误2){
System.out.println(“坐标应该在1到3之间!”);
}
}
谢谢,,
Florin调试代码的最佳方法是堆栈跟踪 尝试添加
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!");
}
这样你就可以追踪你的问题
希望能有所帮助
祝你有愉快的一天:)请阅读:为什么你认为在尝试之前要执行捕获?提示:除非在前面的尝试中出现错误,否则无法输入catch块。因为是先打印“您应该输入数字!”@nomadmaker这只意味着您对输入的数字有问题。请解决它!解决办法是在下一条线之前制作一台新的扫描仪。