Java用for循环输出菱形


思路是建立两个等边三角形,上面的循环5次下面的循环4次,合并成一个菱形。

public static void main(String[] args) {
        for(int i = 1; i <=4 ; i++){
            for(int j = 1; j <= 4; j++){
                if(i + j < 5 ){
                    System.out.print(" ");
                }else{
                    System.out.print(" "+"*");

                }
            }

            System.out.println();
        }

        for(int i = 5; i >=1 ; i--){
            for(int j = 1; j <= 5; j++){
                if(i + j < 6 ){
                    System.out.print(" ");
                }else{
                    System.out.print("*"+" ");

                }
            }
            System.out.println();
        }

    }

运行截图
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_31352839/article/details/81676113
今日推荐