Java 练习:打印一个三角形

Java 练习:打印一个三角形

  • 这里使用的是 For 循环

示例:

package com.shun.struct;

public class TestDemo {
    public static void main(String[] args) {
        //打印一个三角形
        for (int s = 1;s <=5;s++){
            for(int a = 5;a >= s;a--){
                System.out.print(" ");
            }
            for (int d = 1;d <= s;d++){
                System.out.print("*");
            }
            for (int f = 1;f < s;f++){
                System.out.print("*");
            }
            System.out.println();
        }
        /*
        输出的结果是:
                    *
                   ***
                  *****
                 *******
                *********
         */

    }
}
posted @ 2021-07-27 17:07  Liquor无言  阅读(39)  评论(0)    收藏  举报