问个关于java打印菱形的问题代码如下

public class Demo11{
public static void main(String[] args){

int row=20;
int col =row/2;
for(int x=-col;x<=col;x++){
for(int y=-col;y<=col;y++){
if(Math.abs(x)+Math.abs(y)<=col){
System.out.print((char)('a'+Math.abs(y)+Math.abs(x)));
}else{
System.out.print(" ");
}
}
System.out.print("\n");
}

}

}
int col =row/2;这个是干什么用的,什么意思?

因为是菱形,所以从中间来看,是左右对称的,col/2就是确定这个中心点。

那还有x和y都等于-col是什么意思呢?

x代表行,y代表列.........

col = 10;
两个for循环一共运行了21*21=441次。
if条件判断要打印的地方在不在菱形内。在就打印出来。不在就不打印。

public class Demo11{
public static void main(String[] args){

int row=20;
int col =row/2;
for(int x=-col;x<=col;x++){
for(int y=-col;y<=col;y++){
if(Math.abs(x)+Math.abs(y)<=col){
System.out.print((char)('a'+Math.abs(y)+Math.abs(x)));
}else{
System.out.print(" ");
}
}
System.out.print("\n");
}
}
}


你这个是坐标打印菱形的方法,row=20.说明了有20行的打印。col/2就是中间的坐标原点。