黑马程序员技术交流社区
标题:
打印空心菱形和实心菱形
[打印本页]
作者:
zzy张
时间:
2015-9-21 00:49
标题:
打印空心菱形和实心菱形
int hangshu = 11;
int yiban = hangshu / 2 + 1;
int yibanduo = hangshu / 2;
System.out.println("空心菱形:");
for(int k = 1;k <= yiban;k++){
for(int i = 1;i <= (yiban - k);i++){
System.out.print(" ");
}
System.out.print("*");
for(int i = 1;i <= ( (k - 2) * 2 + 1);i++){
System.out.print(" ");
}
if(k != 1){
System.out.print("*");
}
System.out.println();
}
for(int k = yibanduo;k >=1;k--){
for(int i = 1;i <= (yiban - k);i++){
System.out.print(" ");
}
System.out.print("*");
for(int i = 1;i <= ( (k - 2) * 2 + 1);i++){
System.out.print(" ");
}
if(k != 1){
System.out.print("*");
}
System.out.println();
}
System.out.println("实心菱形:");
for(int i = 1;i <= hangshu;i++){
if(i <= hangshu/2 + 1){
for(int k = 1;k <= hangshu/2 + 1- i;k++){
System.out.print(" ");
}
for(int k = 1;k <= i;k++){
System.out.print("* ");
}
System.out.println();
}else{
for(int k =1;k <= (i -(hangshu/2 + 1));k++){
System.out.print(" ");
}
for(int k = 1;k <= (2 *(hangshu/2 + 1) - i);k++){
System.out.print("* ");
}
System.out.println();
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2