如何制作水平金字塔形状的三角形

时间:2017-11-12 16:43:31

标签: java

如何通过java制作这种类型的三角形。 点击查看图片:

enter image description here

这是我现在的代码:

    char a,b,c;

     for (a=1;a<=5;a+=1){
       for (b=0;b<b-a;b--)
         System.out.print(' ');
     for (c=0; c<a; c++){

     System.out.print("* ");
    }
     System.out.println("");
    }
   }
}

1 个答案:

答案 0 :(得分:0)

    for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 7; j++) {
            if ((i < 4 && j >= i + 1 && j < 6 - i) || (i > 3 && j < i && j >= 7 - i)) {
                System.out.print("  ");
            } else {
                System.out.print("* ");
            }
        }
        System.out.println();
    }

在if语句中有两种情况,一种是上部,一种是下部。