仅打印 (n*n) 数组的非边界和角元素

我要编写一个程序,仅打印 (n*n) 数组的非边界和角元素,用于我的作业,这是代码的主要部分: 

https://img4.mukewang.com/64d345f800018fef13450564.jpg

我得到的输出是这样的:

https://img2.mukewang.com/64d3460b0001ad9f05240420.jpg

正如您所看到的,非边界元素(6,7,10,11)没有处于正确的位置,我相信这是因为循环内制表符空格的打印不正确。(我的代码完全是一团糟)我想要一些帮助或建议来解决这个问题。谢谢!



炎炎设计
浏览 126回答 4
4回答

摇曳的蔷薇

我通常发现扁平化事物(特别是 if 条件)并将条件放入布尔返回方法中会有所帮助。尝试类似的东西for (int i = 0; i < n; i++) {&nbsp; for (int j = 0; j < n; j++ {&nbsp; &nbsp; if (isCorner(i,j,n) || !isEdge(i,j,n)) {//...&nbsp; &nbsp; } else {//...&nbsp; &nbsp; }&nbsp; }&nbsp; System.out.println();}其中isCorner(i,j,n)和 的isEdge(i,j,n)定义类似于public boolean isCorner(int row, int column, int gridSize) {//...}

繁花不似锦

如果您找到了解决方案,只是缺少空格,我将添加一些聪明的东西:for (int i = 0; i < n; ++i) {&nbsp; &nbsp; for (int j = 0; j < n; ++j) {&nbsp; &nbsp; &nbsp; &nbsp; boolean visible = (i % (n - 1) == 0) == (j % (n - 1) == 0);&nbsp; &nbsp; &nbsp; &nbsp; if (visible) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf(" %4d", a[i][j]);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("&nbsp; &nbsp; &nbsp;");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println();}尽管我在这里使用了空格,但制表符不再有任何问题"\t"。保持简单,太多的情况只会导致问题 - 正如您所经历的那样。这里的技巧是考虑是否打印。因此我从一个变量开始visible。边界条件i == 0 || i == n - 1也可以用模写为i % (n - 1) == 0如果这“太聪明”了,很难理解阅读:&nbsp; &nbsp; &nbsp; &nbsp; boolean iOnBorder = i % (n - 1) == 0;&nbsp; &nbsp; &nbsp; &nbsp; boolean jOnBorder = j % (n - 1) == 0;&nbsp; &nbsp; &nbsp; &nbsp; boolean visible = iOnBorder == jOnBorder;“X”模式检查 i-on-border 和 j-on-border 的 _equivalence。其余的:格式化的 printf 允许填充数字。

红颜莎娜

试试这个我已经优化了你的 if 条件不需要再次检查i == 0ori == n-1for(int i=0;i<n;i++){&nbsp; &nbsp; &nbsp; &nbsp; for(int j=0;j<n;j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(i==0 || i==n-1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(j==0 || j==n-1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(a[i][j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(j != 0 && j!= n-1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(a[i][j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("\t");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; }

catspeake

试试这个我已经优化了你的 if 条件不需要再次检查i == 0ori == n-1for(int i=0;i<n;i++){&nbsp; &nbsp; &nbsp; &nbsp; for(int j=0;j<n;j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(i==0 || i==n-1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(j==0 || j==n-1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(a[i][j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(j != 0 && j!= n-1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(a[i][j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("\t");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java