用星星画菱形--Java

用星星画菱形

public class Hello{
    public static void main(String[] args) {
        char star = '\u2605';
        System.out.println("Hello " + star + " World!");

        // output a diamond shape with stars
        int temp1 = 0, temp2 = 0;
        for(int i=1; i<=4; ++i){
            temp1 = 4-i;
            temp2 = 2*(i-1)+1;
            while(temp1 > 0){
                System.out.print("  "); // one star char width = two spacing
                temp1--;
            }
            while(temp2 > 0){
                System.out.print(star);
                temp2--;
            }
            System.out.println();
        }

        for(int j=1; j<=3; ++j){
            temp1 = j;
            temp2 = 2*(3-j)+1;
            while(temp1 > 0){
                System.out.print("  ");
                temp1--;
            }
            while(temp2 > 0){
                System.out.print(star);
                temp2--;
            }
            System.out.println();
        }
    }
}

星星是个符号,其宽度有两个空格大小,utf-8编码为:\u2605, 代码输出结果为:

完!

 

posted @ 2018-05-06 16:48  niokit  阅读(864)  评论(0编辑  收藏  举报