【问题标题】:Display squares of asterisks, filled and hollow, side by side并排显示星号正方形,实心和空心
【发布时间】:2018-04-29 19:18:12
【问题描述】:

我目前正在尝试显示一个实心星号方块和一个空心星号:

********** **********
********** *        *
********** *        *
********** *        *
********** **********

相反,我得到了这个输出:

**********
**********
**********
**********
**********
**********
*        *
*        *
*        *
**********

我不太确定如何将其并排放置而不是上下放置。我必须将空心嵌套循环放在填充循环内还是必须做其他事情?这是我的代码:

public class doubleNestedLoops {
    public static void main(String[] args) {
        //Filled Square
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= 10; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

        //Hollow Square
        for (int j = 1; j <= 5; j++) {
            for (int i = 1; i <= 10; i++) {
                if (j == 1 || j == 5 || i == 1 || i == 10) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

【问题讨论】:

    标签: java loops nested-loops ascii-art


    【解决方案1】:

    打印一行后,您无法返回到已打印的行并在其中添加更多 *。这意味着在为填充的正方形打印一行后,您无法返回已打印的同一行为空心正方形添加 *s。

    解决此问题的最简单方法是稍微重构代码,这样您就不必先打印填充的正方形,然后再打印空心的正方形,而是在每一行同时打印两者。

    为此,您可以将代码重组为如下形式:

    //Loop through each line that needs to be printed
    for(int i = 1; i <= 5; i++){
    
       //Loop through characters for filled square needed in this line
       for(int j=0; j< ; j++){
           //Add print logic for filled square
       }
    
       //Loop through characters for hollow square on the same line
       for(int j=1; j<=5; j++){
           //Add print logic for hollow square
       }
    
       System.out.println();//Go to next line
    }
    

    【讨论】:

      【解决方案2】:

      John Martinez 的回答效果很好,但效率有点低。我将在您的代码下方一步一步地进行修改:

      public class Main {
          public static void main(String[] args) {
              //Both Squares
              StringBuilder filledLine = new StringBuilder(10);
              StringBuilder hollowLine = new StringBuilder(10);
              for (int i = 1; i <= 5; i++) {
                  for (int j = 1; j <= 10; j++) {
                      filledLine.append("*");
                      if (i == 1 || i == 5 || j == 1 || j == 10) {
                          hollowLine.append("*");
                      } else {
                          hollowLine.append(" ");
                      }
                  }
                  System.out.println(filledLine + " " + hollowLine);
                  filledLine.delete(0, filledLine.length());
                  hollowLine.delete(0, hollowLine.length());
              }
          }
      }
      

      第 1 步:将两个循环转换为一个循环。这是因为换行后就不能在同一行上打印了。

      第二步:因为我们将在循环中使用字符串,所以使用StringBuffer 更有效,所以我们不会不断地创建新字符串

      第 3 步:将逻辑的所有输出写入缓冲区而不是控制台。

      第 4 步:在填充缓冲区时一次打印一行。

      第 5 步:盈利!

      【讨论】:

        【解决方案3】:

        您可以编写更通用的方法来打印这样的正方形:

        **********  **********  **********  **********  **********  **********  **********
        **********  *        *  *        *  **********  *        *  *        *  **********
        **********  *        *  *        *  **********  *        *  *        *  **********
        **********  *        *  *        *  **********  *        *  *        *  **********
        **********  **********  **********  **********  **********  **********  **********
        
        ******** ******** ******** ******** ******** ********
        *      * ******** *      * *      * ******** *      *
        *      * ******** *      * *      * ******** *      *
        ******** ******** ******** ******** ******** ********
        
        line1: [1, 0, 0, 1, 0, 0, 1], size: 5
        line2: [0, 1, 0, 0, 1, 0], size: 4
        

        Try it online!

        正方形的定义为int[]数组,其中正方形是:0 - 空心和1 - 填充。每个 square 是指定sizeString[] 数组,其中每个字符串的长度是指定size 的两倍。这些方块按顺序并排彼此连接,它们之间有指定的delimiter

        public static void main(String[] args) {
            int[] arr1 = {1, 0, 0, 1, 0, 0, 1};
            int[] arr2 = {0, 1, 0, 0, 1, 0};
        
            Arrays.stream(line(arr1, 5, "  ")).forEach(System.out::println);
            Arrays.stream(line(arr2, 4, " ")).forEach(System.out::println);
        
            System.out.println("line1: " + Arrays.toString(arr1) + ", size: " + 5);
            System.out.println("line2: " + Arrays.toString(arr2) + ", size: " + 4);
        }
        
        public static String[] line(int[] arr, int size, String delimiter) {
            return Arrays.stream(arr)
                    // prepare a square for each number:
                    // 0 - hollow and 1 - filled
                    // Stream<String[]>
                    .mapToObj(i -> square(size, i != 0))
                    // sequentially join squares side-by-side
                    // with the specified delimiter between them
                    .reduce((arr1, arr2) -> IntStream.range(0, size)
                            .mapToObj(i -> arr1[i] + delimiter + arr2[i])
                            .toArray(String[]::new))
                    .orElse(null);
        }
        
        public static String[] square(int size, boolean filled) {
            return IntStream.range(0, size)
                    // i - height, j - width
                    .mapToObj(i -> IntStream.range(0, size * 2)
                            // the filled square and the borders of the hollow
                            // square are asterisks, otherwise whitespaces
                            .mapToObj(j -> filled || i == 0 || j == 0
                                    || i == size - 1 || j == size * 2 - 1 ? "*" : " ")
                            .collect(Collectors.joining()))
                    .toArray(String[]::new);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-08
          • 2014-05-19
          • 2014-11-13
          • 1970-01-01
          • 2014-05-23
          相关资源
          最近更新 更多