假如给定4行,据计算一行空格加星号为9,即n = 9,代码如下:
public class JavaApplication { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int n = 9; int a = 1, b; for(int i = 0; i < 4; i++) { b = n - a; for(int j = 0; j < b/2; j++) { System.out.print(" "); } for(int j = 0; j < a; j++) { System.out.print("*"); } for(int j = 0; j < b/2; j++) { System.out.print(" "); } System.out.println(); a = a+2; } } }