按照你的要求用*显示一个菱形的Java程序如下:
public class H {
public static void main(String[] args) {
final int N=4;
for (int i = 1; i <= N*2-1; i++) {
for (int j = 1; j <= Math.abs(N - i); j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2*N-1-Math.abs(i-N)*2; k++) {
if(k==1 || k == 2*N-1-Math.abs(i-N)*2)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
运行结果:
*
* *
* *
* *
* *
* *
*