我来打印个菱形吧!
/**
需求:打印菱形
*/
class ShapeDemo
{
public static void main(String[] args)
{
shape(6);
}
public static void shape (int a)
{
//外循环控制行
for (int i = 0;i <a ;i++ )
{
//控制每行打印空格个数
for (int j = 0;j<a-i ;j++ )
{
//打印空格
System.out.print(" ");
}
//控制每行打印*的个数
for (int x = 0;x<=i ;x++ )
{
//打印*,*后面加空格可排列出等腰形状
System.out.print("*"+" ");
}
//打印每行后换行
System.out.println();
}
for (int i=a-1;i>0 ;i-- )
{
for (int j = 0;j <=a-i ;j++ )
{
System.out.print(" ");
}
for (int x = 0;x<i ;x++ )
{
System.out.print("*"+" ");
}
System.out.println();
}
}
}
|