java 实现三角形平行四边形等


记得刚开始学习编程的时候,有段时间一直在练习三角形平行四边形这些;
但是这些东西过段时间就会逻辑不清晰,今天回顾了一下,顺便做个记录.
System.out.println("直角在左下的等腰直角三角形");
        for (int i = 1; i <= 9; i++) {
            String row = "";
            for (int j = 1; j <= i; j++) {
               row += " * "; 
            }
             System.out.println(row); 
            } 
System.out.println("--------------------------------------------------------------"); 
            System.out.println("直角在左上的等腰直角三角形"); 
            for (int i = 9; i >= 1; i--) {
            String row = "";
            for (int j = 1; j <= i; j++) {
                row += " * "; 
            } 
            System.out.println(row); 
            } 
System.out.println("--------------------------------------------------------------"); 
            System.out.println("直角在右上的等腰直角三角形"); 
            for (int i = 9; i >= 1; i--) {
            String row = "";
            for (int ij = 9; ij > i; ij--) {
                row += "   ";
            }
            for (int j = 1; j <= i; j++) {
                row += " * ";
            }
            System.out.println(row);
        }
        System.out.println("--------------------------------------------------------------");

        System.out.println("直角在右下的等腰直角三角形");
        for (int i = 1; i <= 9; i++) { 
           String row = ""; 
          for (int ij = 9; ij > i; ij--) {
                row += "   ";
            }
            for (int j = 1; j <= i; j++) {
                row += " * ";
            }
            System.out.println(row);
        }
        System.out.println("--------------------------------------------------------------");

        System.out.println("底边在下的等腰三角形");
        for (int i = 1; i <= 7; i++) { 
            String row = ""; 
            for (int j = 7; j > i; j--) {
                row += "   ";
            }
            for (int ij = 1; ij <= (i * 2 - 1); ij++) {
                row += " * ";
            }
            System.out.println(row);
        }
        System.out.println("--------------------------------------------------------------");

        System.out.println("等边三角形");
        for (int i = 1; i <= 6; i++) { 
           String row = ""; 
            for (int j = 6; j > i; j--) {
                row += " ";
            }
            for (int ij = 1; ij <= i; ij++) {
                if (ij % 2 != 0) {
                    row += "*";
                } else {
                    row += " * ";
                }
            }
            System.out.println(row);
        }
        System.out.println("--------------------------------------------------------------");

        System.out.println("向右偏的平行四边形");
        for (int i = 1; i <= 6; i++) { 
            String row = ""; 
            for (int j = 6; j > i; j--) {
                row += "   ";
            }
            for (int ij = 1; ij <= 6; ij++) {
                row += " * ";
            }
            System.out.println(row);
        }
        System.out.println("--------------------------------------------------------------");

        System.out.println("空心平行四边形");
        //上半部分逻辑
        for (int i = 1; i <= 6; i++) { 
         String row = ""; 
        //左线前的空格逻辑 
         for (int j = 6; j > i; j--) {
                row += "   ";
            }
            //左线逻辑
            for (int ij = 1; ij < 2; ij++) {
                row += " * ";
            }
            //中心逻辑
            for (int ij2 = 1; ij2 < (i * 2 - 2); ij2++) {
                row += "   ";
            }
            //右线逻辑
            for (int ij = 1; ij < 2; ij++) {
                if (i != 1) {
                    row += " * ";
                }
            }
            System.out.println(row);
        }
        //下半部分逻辑
        for (int i = 1; i <= 5; i++) {
            String row = "";
            //左线前空格逻辑
            for (int j = 1; j <= i; j++) {
                row += "   ";
            }
            //左线逻辑
            for (int j = 1; j < 2; j++) {
                row += " * ";
            }
            //中间空白逻辑
            for (int ij = 1; ij < (5 - i) * 2; ij++) {
                row += "   ";
            }
            //右线逻辑
            for (int j = 1; j < 2; j++) {
                if (i != 5) {
                    row += " * ";
                }
            }
            System.out.println(row);
        }
        System.out.println("--------------------------------------------------------------");
测试结果如下:
直角在左下的等腰直角三角形
 * 
 * * 
 * * * 
 * * * * 
 * * * * * 
 * * * * * * 
 * * * * * * * 
 * * * * * * * * 
 * * * * * * * * * 
--------------------------------------------------------------
直角在左上的等腰直角三角形
 * * * * * * * * * 
 * * * * * * * * 
 * * * * * * * 
 * * * * * * 
 * * * * * 
 * * * * 
 * * * 
 * * 
 * 
--------------------------------------------------------------
直角在右上的等腰直角三角形
 * * * * * * * * * 
 * * * * * * * * 
 * * * * * * * 
 * * * * * * 
 * * * * * 
 * * * * 
 * * * 
 * * 
 * 
--------------------------------------------------------------
直角在右下的等腰直角三角形
 * 
 * * 
 * * * 
 * * * * 
 * * * * * 
 * * * * * * 
 * * * * * * * 
 * * * * * * * * 
 * * * * * * * * * 
--------------------------------------------------------------
底边在下的等腰三角形
 * 
 * * * 
 * * * * * 
 * * * * * * * 
 * * * * * * * * * 
 * * * * * * * * * * * 
 * * * * * * * * * * * * * 
--------------------------------------------------------------
等边三角形
     *
    * * 
   * * *
  * * * * 
 * * * * *
* * * * * *  
--------------------------------------------------------------
向右偏的平行四边形
           * * * * * * 
         * * * * * * 
       * * * * * * 
     * * * * * * 
   * * * * * * 
 * * * * * * 
--------------------------------------------------------------
空心平行四边形
           * 
         *   * 
       *       * 
     *           * 
   *               * 
 *                   * 
   *               * 
     *           * 
       *       * 
         *   * 
           * 
--------------------------------------------------------------