标签:blog http java ar for 2014 log on c
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.一般两层循环,外层循环控制行数。外循环的变量在内循环中必须有!
输入N,打印如图所示的三角形(例:N=3,N=4,N=5)1<=N<=26
标签:blog http java ar for 2014 log on c
原文地址:http://blog.csdn.net/so_sss/article/details/39338107