如何让我的对角线从星号框的左下方开始打印?

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

how do I get my diagonal line to start printing from the bottom left of my asterik box?

问题

我正在编写一个代码,用于练习创建方法并在主方法中调用这些方法。我在输出方面遇到了问题,输出结果与预期不符。代码应该根据输入的数字9创建一个方框。以下是我的代码输出:

  1. *********
  2. *+ *
  3. * + *
  4. * + *
  5. * + *
  6. * + *
  7. * + *
  8. * +*
  9. *********

我无法弄清楚如何使斜线“+”从方框的左下角开始,到达右上角。我的代码如下:

  1. public static void main(String[] args) {
  2. System.out.print(boxWithMinorDiagonal(9));
  3. }
  4. public static int boxWithMinorDiagonal(int n) {
  5. for (int col = 1; col <= n; col++) {
  6. for (int row = 1; row <= n; row++) {
  7. if (row == 1 || col == 1 || row == n || col == n) {
  8. System.out.print("*");
  9. } else if (col == row) {
  10. System.out.print("+");
  11. } else {
  12. System.out.print(" ");
  13. }
  14. }
  15. System.out.println();
  16. }
  17. return n;
  18. }

我认为我需要让行(row)和列(col)从左下角开始,从那里开始斜线,但我不确定我需要在for循环中做哪些更改,以使行和列从左下角开始。

英文:

I am making a code that is to practice for creating methods and calling them in the main method. I am having trouble with an output that is not exactly how it is supposed to look like. The code is supposed to create a box based on the input 9. This is the output of my code:

  1. *********
  2. *+ *
  3. * + *
  4. * + *
  5. * + *
  6. * + *
  7. * + *
  8. * +*
  9. *********

I can't figure out how to get the "+" diagonal to start from the bottom left of the box to the top right. my code is this:

  1. public static void main(String[] args)
  2. {
  3. System.out.print(boxWithMinorDiagonal(9));
  4. }
  5. public static int boxWithMinorDiagonal(int n){
  6. for (int col = 1; col &lt;= n; col++) {
  7. for (int row = 1; row &lt;= n; row++) {
  8. if(row == 1 || col == 1 || row == n || col == n){
  9. System.out.print(&quot;*&quot;);
  10. }
  11. else if (col == row){
  12. System.out.print(&quot;+&quot;);
  13. }
  14. else{
  15. System.out.print(&quot; &quot;);
  16. }
  17. }
  18. System.out.println();
  19. }
  20. return n;
  21. }

I think I need to get the row and col to start from the bottom left, which would start the diagonal from there, but am not exactly sure what I would need to change in my for loops to get the row and col to start from the bottom left.

答案1

得分: 1

现在,如果例如 col 为 1 且 row 为 1,或者 col 为 2 且 row 为 2,你会绘制一个星星。

制作一个表格,列出应该在从左下到右上方向上产生星星的行/列组合,然后尝试找出其中的规律,可以用数学运算的方式来表示,以返回一个布尔值。

提示:col + row == something 可能是你想要查看的内容。

英文:

Right now you draw a star if e.g. the col is 1 and the row is 1, or the col is 2 and the row is 2.

Make a table of which row/col combos should produce a star for bottom-left to top-right, then try to find a pattern in this you can put in terms of a mathematical operation that returns a boolean.

HINT: col + row == something might be something you want to check out.

答案2

得分: 0

只需更改这部分代码:else if (col == n-row+1){ System.out.print(&quot;+&quot;);}
那么你的代码将变为:

  1. {
  2. System.out.print(boxWithMinorDiagonal(9));
  3. }
  4. public static int boxWithMinorDiagonal(int n){
  5. for (int col = 1; col <= n; col++) {
  6. for (int row = 1; row <= n; row++) {
  7. if(row == 1 || col == 1 || row == n || col == n){
  8. System.out.print(&quot;*&quot;);
  9. }
  10. else if (col == n-row+1){
  11. System.out.print(&quot;+&quot;);
  12. }
  13. else{
  14. System.out.print(&quot; &quot;);
  15. }
  16. }
  17. System.out.println();
  18. }
  19. return n;
  20. }

然后你将获得以下输出:

  1. *********
  2. * +*
  3. * + *
  4. * + *
  5. * + *
  6. * + *
  7. * + *
  8. *+ *
  9. *********
  10. 9
英文:

just change this part :else if (col == n-row+1){ System.out.print(&quot;+&quot;);}
So your code will be :

  1. {
  2. System.out.print(boxWithMinorDiagonal(9));
  3. }
  4. public static int boxWithMinorDiagonal(int n){
  5. for (int col = 1; col &lt;= n; col++) {
  6. for (int row = 1; row &lt;= n; row++) {
  7. if(row == 1 || col == 1 || row == n || col == n){
  8. System.out.print(&quot;*&quot;);
  9. }
  10. else if (col == n-row+1){
  11. System.out.print(&quot;+&quot;);
  12. }
  13. else{
  14. System.out.print(&quot; &quot;);
  15. }
  16. }
  17. System.out.println();
  18. }
  19. return n;

and you'll get this:

  1. *********
  2. * +*
  3. * + *
  4. * + *
  5. * + *
  6. * + *
  7. * + *
  8. *+ *
  9. *********
  10. 9

huangapple
  • 本文由 发表于 2020年10月28日 08:08:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64564665.html
匿名

发表评论

匿名网友

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

确定