public class ling {
public static void main (String[]args)
{
int lay;
lay = 7;//菱形的总长度
for(int m = 1; m <=(lay+1)/2; m++)//正序输出上半部分
{
for(int b = 1; b <=(lay+1)/2-m ; b++)//输出空格
{
System.out.print(" ");
}
for(int c = 1; c <= m*2-1; c++)
{
System.out.print("*");
}
System.out.println();//换行
}
for(int d =(lay+1)/2-1;d >= 1; d --)//倒序输出下半部分
{
for(int b = 1; b <= (lay+1)/2-d; b++)//输出空格
{
System.out.print(" ");
}
for(int c = (lay+1)/2-d; c <=(lay+1)/2-2+d; c ++)//(lay+1)/2-1即为下半个三角形
{
System.out.print("*");
}
System.out.println();
}
}
public static void main (String[]args)
{
int lay;
lay = 7;//菱形的总长度
for(int m = 1; m <=(lay+1)/2; m++)//正序输出上半部分
{
for(int b = 1; b <=(lay+1)/2-m ; b++)//输出空格
{
System.out.print(" ");
}
for(int c = 1; c <= m*2-1; c++)
{
System.out.print("*");
}
System.out.println();//换行
}
for(int d =(lay+1)/2-1;d >= 1; d --)//倒序输出下半部分
{
for(int b = 1; b <= (lay+1)/2-d; b++)//输出空格
{
System.out.print(" ");
}
for(int c = (lay+1)/2-d; c <=(lay+1)/2-2+d; c ++)//(lay+1)/2-1即为下半个三角形
{
System.out.print("*");
}
System.out.println();
}
}
}
心得体会
今天写这个的主要目的在于,使我了解了双重for循环的作用.其中第一个for循环主要是控制行,而第二个for主要是控制列.然后就是一种算法,算法在编程中起到了决定性的作用.