日期:2014-05-20 浏览次数:20642 次
public class csdn { public static void main(String[] args) throws Throwable { int n = 5, m = 5; int center = n * m; StringBuilder buf = new StringBuilder(); //用来保存字符* for (int i=0; i<n; i++) { //大三角形循环 for (int j=0; j<m; j++) { //小三角形循环 buf.delete(0, buf.length()); //清空字符* for (int k=0; k<2*j+1; k++) { //计算并保存小三角形每层的字符* buf.append("*"); } for (int k=0, p=center-i*m+j; k<=i; k++, p=2*m) { //大三角形的每层循环 String format = "%" + p + "s"; //计算开始位置并设置格式化输出 System.out.printf(format, buf.toString()); //打印 } System.out.println(); //小三角形每层换行 } } } }