?//打印棱形rhombus
?public static void printRhombus(int height){
???? int top=height/2+1;//先计算出上部分的行数
??for(int x=1;x<=top;x++){
??//上部分的高每循环一次少打印一个空格;等于高减掉循环次数
???for(int i=1; i<=top-x;i++){
????System.out.print(" ");
???}
???//那么上部分高的*等于循环次数*2-1
???for(int y=1;y<=x*2-1;y++){
????System.out.print("*");
???}
???System.out.println();
??}
??
??int bottom=height-top;//计算出下部分的行数
??for(int i=1;i<=bottom;i++){
???? for(int x=1;x<=i;x++){
???? //这里的空格是随着每循环一次多打印一个
????System.out.print(" ");
???}
?//通过bottom*2-i*2+1可以计算出*号每循环一次要打印的个数
???for(int y=1;y<=bottom*2-i*2+1;y++){
????System.out.print("*");
???}
??System.out.println();
??}
??
?}
?
原文:http://duxubo511325.iteye.com/blog/2176964