Ultimate Diamond Maker工具指南

时间:2019-03-20 09:09:48

标签: java

我注意到许多人都在寻找如何制作钻石图案的方法,因此我正在研究一种最终的钻石制作工具。该工具接受钻石的长度,要在钻石中打印的左右字符串。

这是我到目前为止所拥有的:

public class Main {
    public static void main(String[] args) {
        int iMax = 10;
        String left = "/";
        String right = "\\";

        // Print the upper part
        for (int i = 0; i < iMax; i++) {
            for (int j = 0; j <= iMax - i; j++) {
                System.out.print(" ");
            }
            for (int j = iMax - i; j <= iMax; j++) {
                System.out.print(left);
            }
            for (int j = iMax - i; j <= iMax; j++) {
                System.out.print(right);
            }
            System.out.println();
        }

        // Print the lower part
        for (int i = 0; i < iMax; i++) {
            for (int j = iMax - (i + 1); j <= iMax; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= iMax - i; j++) {
                System.out.print(right);
            }
            for (int j = 1; j <= iMax - i; j++) {
                System.out.print(left);
            }
            System.out.println();
        }

    }
}

这是结果:

           /\
          //\\
         ///\\\
        ////\\\\
       /////\\\\\
      //////\\\\\\
     ///////\\\\\\\
    ////////\\\\\\\\
   /////////\\\\\\\\\
  //////////\\\\\\\\\\
  \\\\\\\\\\//////////
   \\\\\\\\\/////////
    \\\\\\\\////////
     \\\\\\\///////
      \\\\\\//////
       \\\\\/////
        \\\\////
         \\\///
          \\//
           \/

它正在工作,但是我希望它更加灵活。因此,任何人都可以帮助我修改此代码,使其看起来更具可读性。预先感谢。

1 个答案:

答案 0 :(得分:0)

我已经用这种方式实现了。这只是对那些喜欢钻石图案的人的指导。这是代码:


public class UltimateDiamondMaker {
    public static void main(String[] args) {
        print(10, "(", ")");
    }

    /*
     * This method will print the diamond
     *
     * @param iMax the half of the length of the diamond
     *
     * @param left the string to be printed on the left side of the diamond
     *
     * @param right the string to be printed on the right side of the diamond
     */
    public static void print(int iMax, String left, String right) {
        printUp(iMax, left, right);
        printDown(iMax, left, right);
    }

    /*
     * This method will print upper part the diamond
     *
     * @param iMax the half of the length of the diamond
     *
     * @param left the string to be printed on the left side of the diamond
     *
     * @param right the string to be printed on the right side of the diamond
     */
    public static void printUp(int iMax, String left, String right) {
        for (int i = 0; i < iMax; i++) {
            printHelper(0, iMax - i, " ");
            printHelper(iMax - i, iMax, left);
            printHelper(iMax - i, iMax, right);
            System.out.println();
        }
    }

    /*
     * This method will print lower part the diamond
     *
     * @param iMax the half of the length of the diamond
     *
     * @param left the string to be printed on the left side of the diamond
     *
     * @param right the string to be printed on the right side of the diamond
     */
    public static void printDown(int iMax, String left, String right) {
        for (int i = 0; i < iMax; i++) {
            printHelper(iMax - (i + 1), iMax, " ");
            printHelper(1, iMax - i, left);
            printHelper(1, iMax - i, right);
            System.out.println();
        }
    }

    /*
     * This method is a helper method. This will print a single line in the diamond
     *
     * @param start the start index
     *
     * @param end the end index
     *
     * @param s the string to be printed
     */
    public static void printHelper(int start, int end, String s) {
        for (int j = start; j <= end; j++) {
            System.out.print(s);
        }
    }
}

输出:

           ()
          (())
         ((()))
        (((())))
       ((((()))))
      (((((())))))
     ((((((()))))))
    (((((((())))))))
   ((((((((()))))))))
  (((((((((())))))))))
  (((((((((())))))))))
   ((((((((()))))))))
    (((((((())))))))
     ((((((()))))))
      (((((())))))
       ((((()))))
        (((())))
         ((()))
          (())
           ()