Try{} Catch {}在Java中执行catch之前执行{} try{}

huangapple go评论91阅读模式
英文:

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*

运行后,我在输入坐标之前捕获到异常消息。为什么?我应该更改什么?

  1. Scanner scanner = new Scanner(System.in);
  2. String[][] tictactoe = new String[3][3];
  3. // 初始化方法
  4. System.out.print("输入单元格:");
  5. String s = scanner.next();
  6. String a = s.substring(0, 1);
  7. tictactoe[0][0] = a;
  8. String b = s.substring(1, 2);
  9. tictactoe[0][1] = b;
  10. String c = s.substring(2, 3);
  11. tictactoe[0][2] = c;
  12. String d = s.substring(3, 4);
  13. tictactoe[1][0] = d;
  14. String e = s.substring(4, 5);
  15. tictactoe[1][1] = e;
  16. String f = s.substring(5, 6);
  17. tictactoe[1][2] = f;
  18. String g = s.substring(6, 7);
  19. tictactoe[2][0] = g;
  20. String h = s.substring(7, 8);
  21. tictactoe[2][1] = h;
  22. String i = s.substring(8, 9);
  23. tictactoe[2][2] = i;
  24. for (int n = 0; n < 3; n++) {
  25. for (int m = 0; m < 3; m++) {
  26. String cuv = tictactoe[n][m];
  27. if (cuv.equals("_")) {
  28. tictactoe[n][m] = " ";
  29. }
  30. }
  31. }
  32. System.out.println("---------");
  33. System.out.println("| " + tictactoe[0][0] + " " + tictactoe[0][1] + " " + tictactoe[0][2] + " |");
  34. System.out.println("| " + tictactoe[1][0] + " " + tictactoe[1][1] + " " + tictactoe[1][2] + " |");
  35. System.out.println("| " + tictactoe[2][0] + " " + tictactoe[2][1] + " " + tictactoe[2][2] + " |");
  36. String player1 = "X";
  37. String letter;
  38. boolean correctCoordinate = false;
  39. while (!correctCoordinate) {
  40. System.out.print("输入坐标:");
  41. String input = scanner.nextLine();
  42. String[] pieces = input.trim().split("\\s+");
  43. int x;
  44. int y;
  45. try {
  46. x = Integer.parseInt(pieces[0]);
  47. y = Integer.parseInt(pieces[1]);
  48. letter = tictactoe[3 - y][x - 1];
  49. if (letter.equals("X") || letter.equals("O")) {
  50. System.out.println("该单元格已被占用!请选择其他单元格!");
  51. } else {
  52. tictactoe[3 - y][x - 1] = player1;
  53. System.out.println("---------");
  54. System.out.println("| " + tictactoe[0][0] + " " + tictactoe[0][1] + " " + tictactoe[0][2] + " |");
  55. System.out.println("| " + tictactoe[1][0] + " " + tictactoe[1][1] + " " + tictactoe[1][2] + " |");
  56. System.out.println("| " + tictactoe[2][0] + " " + tictactoe[2][1] + " " + tictactoe[2][2] + " |");
  57. System.out.println("---------");
  58. correctCoordinate = true;
  59. }
  60. } catch (NumberFormatException err1) {
  61. System.out.println("您应该输入数字!");
  62. } catch (ArrayIndexOutOfBoundsException err2) {
  63. System.out.println("坐标应该在1到3之间!");
  64. }
  65. }

谢谢,
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];

  1. //init method
  2. System.out.print(&quot;Enter cells: &quot;);
  3. String s = scanner.next();
  4. String a = s.substring(0, 1);
  5. tictactoe[0][0] = a;
  6. String b = s.substring(1, 2);
  7. tictactoe[0][1] = b;
  8. String c = s.substring(2, 3);
  9. tictactoe[0][2] = c;
  10. String d = s.substring(3, 4);
  11. tictactoe[1][0] = d;
  12. String e = s.substring(4, 5);
  13. tictactoe[1][1] = e;
  14. String f = s.substring(5, 6);
  15. tictactoe[1][2] = f;
  16. String g = s.substring(6, 7);
  17. tictactoe[2][0] = g;
  18. String h = s.substring(7, 8);
  19. tictactoe[2][1] = h;
  20. String i = s.substring(8, 9);
  21. tictactoe[2][2] = i;
  22. for (int n = 0; n &lt; 3; n++) {
  23. for (int m = 0; m &lt; 3; m++) {
  24. String cuv = tictactoe[n][m];
  25. if (cuv.equals(&quot;_&quot;)) {
  26. tictactoe[n][m] =&quot; &quot;;
  27. }
  28. }
  29. }
  30. System.out.println(&quot;---------&quot;);
  31. System.out.println(&quot;| &quot; + tictactoe[0][0] + &quot; &quot; + tictactoe[0][1] + &quot; &quot; + tictactoe[0][2] + &quot; |&quot;);
  32. System.out.println(&quot;| &quot; + tictactoe[1][0] + &quot; &quot; + tictactoe[1][1] + &quot; &quot; + tictactoe[1][2] + &quot; |&quot;);
  33. System.out.println(&quot;| &quot; + tictactoe[2][0] + &quot; &quot; + tictactoe[2][1] + &quot; &quot; + tictactoe[2][2] + &quot; |&quot;);
  34. System.out.println(&quot;---------&quot;);
  35. String player1 = &quot;X&quot;;
  36. String letter;
  37. boolean correctCoordinate=false;
  38. while (!correctCoordinate){
  39. System.out.print(&quot;Enter the coordinates:&quot;);
  40. String input=scanner.nextLine();
  41. String [] pieces = input.trim().split(&quot;\\s+&quot;);
  42. int x;
  43. int y;
  44. try {
  45. x = Integer.parseInt(pieces[0]);
  46. y = Integer.parseInt(pieces[1]);
  47. letter = tictactoe[3-y][x-1];
  48. if (letter.equals(&quot;X&quot;) || letter.equals(&quot;O&quot;)) {
  49. System.out.println(&quot;This cell is occupied! Choose another one!&quot;);
  50. } else {
  51. tictactoe[3-y][x-1]=player1;
  52. System.out.println(&quot;---------&quot;);
  53. System.out.println(&quot;| &quot; + tictactoe[0][0] + &quot; &quot; + tictactoe[0][1] + &quot; &quot; + tictactoe[0][2] + &quot; |&quot;);
  54. System.out.println(&quot;| &quot; + tictactoe[1][0] + &quot; &quot; + tictactoe[1][1] + &quot; &quot; + tictactoe[1][2] + &quot; |&quot;);
  55. System.out.println(&quot;| &quot; + tictactoe[2][0] + &quot; &quot; + tictactoe[2][1] + &quot; &quot; + tictactoe[2][2] + &quot; |&quot;);
  56. System.out.println(&quot;---------&quot;);
  57. correctCoordinate=true;
  58. }
  59. }catch (NumberFormatException err1) {
  60. System.out.println(&quot;You should enter numbers!&quot;);
  61. }catch (ArrayIndexOutOfBoundsException err2){
  62. System.out.println(&quot;Coordinates should be from 1 to 3!&quot;);
  63. }
  64. }

Thank you,
Florin

答案1

得分: 1

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

尝试添加以下代码块:

  1. catch (NumberFormatException err1) {
  2. err1.printStackTrace();
  3. System.out.println("您应该输入数字!");
  4. } catch (ArrayIndexOutOfBoundsException err2) {
  5. err2.printStackTrace();
  6. System.out.println("坐标应该在1到3之间!");
  7. }

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

希望这对你有帮助。

祝你有一个愉快的一天 Try{} Catch {}在Java中执行catch之前执行{} try{}

英文:

The best way to debug your code is by stack tracing.

Try adding

  1. catch (NumberFormatException err1) {
  2. err1.printStackTrace();
  3. System.out.println(&quot;You should enter numbers!&quot;);
  4. }catch (ArrayIndexOutOfBoundsException err2){
  5. err2.printStackTrace();
  6. System.out.println(&quot;Coordinates should be from 1 to 3!&quot;);
  7. }

This way you can trace your issue.

Hope it was helpful.

Have a nice day Try{} Catch {}在Java中执行catch之前执行{} try{}

huangapple
  • 本文由 发表于 2020年8月7日 23:16:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63304624.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定