日期:2014-05-20 浏览次数:20737 次
public class OutputTriangle {// 输出三角 public static void main(String[] args) { int n = 5; String str = " "; for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) System.out.print(str); for (int j = n - i; j > 0; j--) System.out.print("*" + str); System.out.println(); } } } * * * * * * * * * * * * * * *
------解决方案--------------------
public class OutputTriangle {// 输出三角 public static void main(String[] args) { int n = 5; String output=""; String str = " "; for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) output+=str; for (int j = n - i; j > 0; j--) output+="*" + str; output+="\n"; } System.out.println(output); } } * * * * * * * * * * * * * * *
------解决方案--------------------
public class Test{ public static void main(String args[]){ StringBuffer output = new StringBuffer(); for(int i = 0;i<5;i++){ StringBuffer str = new StringBuffer(); for(int j = i;j<5){ str.append("* "); } str.append("\r\n"); output.append(str); } System.out.println(output.toString()); } }