package demo;public class PrintDemo { public static void main(String[] args) { print(26); } private static void print(int i) { if(i<1||i>26){ return; } int j,j2; for (j = 1; j <= i; j++) { for (j2 = 1; j2 <= i; j2++) { if(j+j2<=i){ System.out.print(" "); }else{ System.out.print((char)(64+j2)); } } for (j2 = i-1; j2 >=1; j2--) { if(j+j2>i){ System.out.print((char)(64+j2)); }else { System.out.print(" "); } } System.out.println(); } }}输出结果:
三点:
1.System.out.print()不要写成System.out.println()了。
2.假设在对角线操作。能够用if推断。
3.一般两层循环,外层循环控制行数。外循环的变量在内循环中必须有!