publicclassTest3{publicstaticvoidmain(Stringargs[]){finalintcount=4;for(inti=1;i=count-i+1&&j<=count+i-1){System...
public class Test3 {
public static void main(String args[]) {
final int count = 4;
for (int i = 1; i < count * 2; i++) {
for (int j = 1; j < count * 2; j++) {
if (i <= count) {
if (j >= count - i + 1 && j <= count + i - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
} else {
if (j >= i - count + 1 && j <= 3 * count - i - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
}
System.out.print("\n");
}
}
}
这是用Java嵌套循环输出菱形的例子。请高人解释
final int count = 4;
for (int i = 1; i < count * 2; i++) {
for (int j = 1; j < count * 2; j++) {
if (i <= count) {
if (j >= count - i + 1 && j <= count + i - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
} else {
if (j >= i - count + 1 && j <= 3 * count - i - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
}
这部分的含义,尤其是
for (int j = 1; j < count * 2; j++) {
if (i <= count) {
if (j >= count - i + 1 && j <= count + i - 1) {
System.out.print("*");
这一部分。
展开