java中使用for循环打印输出正三角形

package test;

public class Test {
    public static void main(String[] args) {
        System.out.println("正三角:");
        for(int i = 1; i <= 10; i++){//外层循环控制行数
            //内层循环控制列数
            for (int j = 1; j < 10; j++) {
                if (i<=j) {
                    System.out.print(" ");    
                }
            }
            
            for (int j = 10; j >= 1; j--) {
                if (i>=j) {
                    if (j==10) {
                        System.out.print("*");
                    }else{
                        System.out.print(j);
                    }
                }
            }
            
            for (int j = 1; j <= 10; j++) {
                if (j<=i&&j!=1) {
                    if(j==10){
                        System.out.print("*");
                    }else{
                        System.out.print(j);
                    }
                }
            }
            
            for (int j = 10; j >= 1; j--) {
                if (i<=j) {
                    System.out.print(" ");    
                }
            }
            System.out.println();
        }
        
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_40471291/article/details/80158728