日期:2014-05-20 浏览次数:20822 次
* *** ***** public class Test { public static void main(String[] args) { for(int i=1;i<=3;i++){ for(int j=3-i;j>=1;j--) System.out.print(" "); for(int k=2*i-1;k>0;k--) System.out.print("*"); System.out.println(); } } }
------解决方案--------------------
public class Main { public static void main(String[] args) { for(int i=4;i>=1;i--){ StringBuilder builder=new StringBuilder(" "); for(int j=i;j<=8-i;j+=2){ builder.setCharAt(j, '*'); } System.out.println(builder); } } }
------解决方案--------------------
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class xingxing { public static void main(String[] args) { int lineNum = 0; boolean flag = true; StringBuffer stringBuffer = new StringBuffer(""); while (flag) { try { System.out.print("请输入一个正整数:"); lineNum = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)) .readLine()); flag = false; } catch (IOException ioe) { ioe.printStackTrace(); System.out.println("输入异常!!"); } catch (NumberFormatException nume) { System.out.print("无法转换,请重新输入一个数:"); } if (lineNum < 0) { flag = true; System.out.print("请输入一个正数:"); } } // 组建基础空格串 for (int i = 0; i < lineNum * 2; i++) { stringBuffer.append(" "); } for (int i = lineNum; i >= 1; i--) { StringBuilder builder = new StringBuilder(stringBuffer.toString()); for (int j = i; j <= lineNum * 2 - i; j += 2) { builder.setCharAt(j, '*'); } System.out.println(builder); } } }