代码改变世界

java 实例,打印直角三角形,金字塔

2017-03-20 09:46  backyyan  阅读(1746)  评论(0编辑  收藏  举报

//直角三角形

public class sanjiaoxing {
public static void main(String[] args) {
int i, j, num = 7;
for (i = 1; i <= num; i++) {
System.out.println(" ");
for (j = 1; j <= 2 * i - 1; j++) {
System.out.print("*" + " ");
}
}
}
}

 

//金字塔
public class dysjx {
    public static void main(String[] args) {

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4 - i; j++) {
                System.out.print(" ");
            }
            for (int k = 0; k < 2 * i + 1; k++) {
                System.out.print("*");
            }
            System.out.println();
        }

    }
}