// 計算楊暉三角形,無遞迴版本 import java.io.*; public class a07b{ public static void main(String args[]) throws Exception { //程式進入點 String getbr; int i,j; int a,b;//a階到b階 int tmp; int pad; int cans; BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); while (true) { do { System.out.println("請寫出a階到b階之楊暉三角塔:"); System.out.print("請輸入a "); getbr = br.readLine(); if (getbr.equals("0")) //判斷結束 { System.out.println("結束!"); return; } a=Integer.parseInt(getbr);//取得A階 System.out.print("請輸入b "); getbr = br.readLine(); b=Integer.parseInt(getbr);//取得B階 } while (a<0 || b< 0||a>10||b>10); /********** 處理好a b的大小關係 ********/ if (a>b) { tmp=a; a=b; b=tmp; } for (i=a;i<=b;i=i+1){ { cans=1; System.out.print("1\t"); for (j=1;j<=i;j=j+1) { cans=cans*(i-j+1)/j; System.out.print(cans+"\t"); } } System.out.println(); } System.out.println(); //給程式輸出好看一點的 }//while(true) }//main }