首页 > 技术文章 > 打印三角形及Debug

kazesan 2022-04-21 19:25 原文

打印三角形(5行)

拆分思想

想象成一个矩形,分成四个直角三角形。

第二个直角三角形对称一下即打印出三角形。

public static void main(String[] args){
    for(int i = 1; i <= 5; i++){
        for(int j = 5; j>=i; j--){
           System.out.print("*"); 
        }
        System.out.println();
    }
    /*
    *****
    ****
    ***
    **
    *
    */
}
public class TestDemo {
    public static void main(String[] args) {
        //打印三角形 5行
        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j >= i; j--){
                System.out.print(" ");
            }
            for (int j = 1; j <= i; j++){
                System.out.print("*");
            }
            for (int j = 1; j < i; j++){
                System.out.print("*");
            }
            System.out.println();
        }

    }
    /*
     *
    ***
   *****
  *******
 *********
    */
}

Debug

菜单栏绿色蟑螂图标

推荐阅读