Java 打印模式循环



我已经广泛搜索了我遇到的这个问题的解决方案。我走在正确的轨道上(我认为),但没有正确思考一些逻辑,所以想知道我是否可以为此获得一些帮助。

  for (int i = 0; i < 6; i++)
  {
     for (int j = 0; j < i; j++)
     {
        System.out.print(" ");
     }
     System.out.println("#");
  }
  for (int l = 0; l < 6; l++)
  {
     for (int m = 6; m > l; m--)
     {
        System.out.print(" ");
     }
     System.out.println("#");
  }
Output: #
         #
          #
           #
            #
             #
              #
             #
            #
           #
          #
         #

我想要实现的是 output: # # # # # # # # # # # # # # # # # # #

这里有

一些帮助,在清晰和突出显示的步骤中,因此您可以阅读第一个并尝试,然后再转到下一个...为避免损坏溶液:)

帮助 1

观察模式有点规律,但每行的开头都有一些"噪音"......首先,尽量避免打印初始空格。而不是

     #           #
      #         # #
       #       #   #
        #     #     #
         #   #       #
          # #         #
           #           #

考虑

     #           #
     #         # #
     #       #   #
     #     #     #
     #   #       #
     # #         #
     #           #

帮助 2

显然,从第一#到最后一#,每一行的大小都相同。所以,你必须"移动中间的那个。基本上,这意味着每条线都由一个固定的#、空格、可移动#、空格、另一个固定#组成。

帮助 3

你如何"移动"中间的?空间总数会改变吗?第一个和最后一个#之间的空格数不会更改。因此,您有固定数量的空格(例如 10),但您在两者之间插入了一些内容。空格的模式将如下所示:0#10,1#9,2#8,...,9#1,10#0。你如何通过单个循环做到这一点?

我不会讨论生成所需图形的解决方案是什么。相反,我想评论一下您原始代码中的几个错误。

您发布的代码有 2 个主要的 for 循环。每个,根据您设置的条件:

int i = 0; i < 6; i++

将导致

System.out.println("#");

在六行不同的行上打印 6 个"#"。

由于您有两个执行 println() 的 for 循环,它们将导致程序总共显示 12 行,而不管每个主 for 循环中的子 for 循环如何,因为 sub-for 循环仅用于在打印"#"之前显示空格。

输出存储在字符双精度数组中,然后在数组中移动时替换点。

int height = 6;
int peeks = 5;
// the top and bottom peaks share a point with eachother
// so for every down and up there are "2*height - 2" points
// the last peak has no connection, so add 1
int duration = (peeks*2*(height - 1)) + 1;
// create a double array and fill it with spaces
char[][] output = new char[height][duration];
for(int i = 0; i < height; i++){
    for(int j = 0; j < duration; j++){
        output[i][j] = ' ';
    }
}
// keep track of if we are going up or down
boolean increment = true;
for(int distance = 0, level = 0; distance < duration; distance++){
    // mark every point in the path
    output[level][distance] = '#';
    if (level == 0){
        increment = true;
    }
    else if (level == height -1){
        increment = false;
    }
    level += increment? 1 : -1;
}
// print each line in the double array
for(int i = 0; i < output.length; i++){
    System.out.println(String.valueOf(output[i]));
}

我相信有一个更优雅的解决方案,但这里有某种具有您请求的确切输出的蛮体算法:

   int spaceCount1 = 9;
   int spaceCount2 = 11;
   int spaceCount3 = 0;
   int j = 1;
   for(int row = 0; row < 7; row++){
       System.out.println();
       for(int i = 0; i < spaceCount1; i++){
           System.out.print(" ");
       }
       spaceCount1++;
       for(j = 1; j <= 1; j++){
            System.out.print("#");
       }
       j--;
       for(int i = spaceCount2; i > 0; i--){
            System.out.print(" ");
       }
       spaceCount2 -= 2;
       for(j = 1; j <= 1 && row < 6; j++){
            System.out.print("#");
       }
       j--;
       for(int i = 1; i < spaceCount3 ; i++){
           System.out.print(" ");
       }
       spaceCount3 += 2;
       for(j = 1; j <= 1 && row > 0; j++){
           System.out.print("#");
       }
       j--;
  }

输出:

     #           #
      #         # #
       #       #   #
        #     #     #
         #   #       #
          # #         #
           #           #

最新更新