黑马程序员技术交流社区
标题:
for循环打印菱形
[打印本页]
作者:
散人online
时间:
2013-2-2 13:46
标题:
for循环打印菱形
* 4 x=0 y=4 y=4-x
* * 3 5 x=1 y=3,5 3=4-x 5=4+x
* * 2 6 x=2 y=2,6 2=4-x 6=4+x
* * 1 7 x=3 y=1,7 1=4-x 7=4+x
* * a=2 b=2,6 2=4-a 6=4+a
* * a=1 b=3,5 3=4-a 5=4+a
* a=0 b=4 4=4-a
class ForDemo
{
public static void main(String[] args)
{
for(int x=0; x<4; x++)
{
for(int y=1; y<8; y++)
{
if (y==4-x|y==4+x)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
for(int a=2; a>=0; a--)
{
for(int b=1; b<8; b++)
{
if(b==4-a|b==4+a)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
我是根据*出现的规律通过两个for循环嵌套分上下两部分的拼凑出来的,
可不可以通过一个for循环嵌套打印出来,for中的表达式该如何写?
作者:
施大勇
时间:
2013-9-23 16:47
拷贝了回去研究下,我也做过这样的题,不过我没搞出来。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2