/*********************************************************** write by tjm 用來展示overload ***********************************************************/ import java.io.*; public class demo23{ public static void main(String args[]) throws Exception { //程式進入點 String getbr; BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); int x,pt; double fx; /*****請使用者輸入選項**********/ x=5; while (x>=0) { System.out.print("請輸入要計算階乘的數字:"); getbr = br.readLine(); /*****判斷是否是整數*******/ pt=getbr.indexOf("."); //輸入字串如果有小數點就是實數 if (pt < 0) //整數 { x=Integer.parseInt(getbr); if (x<0) { System.out.println("謝謝!再見!"); continue; } System.out.println(x+ "!="+fraction(x)); } else { fx=Double.parseDouble(getbr); if (fx<0) { System.out.println("謝謝!再見!"); continue; } System.out.println(fx+ "!="+fraction(fx)); } } //while結束 } //main 結束 //整數階乘 private static int fraction(int y) { int ans; ans=1; if (y<=1) ans=1; else ans=y*fraction(y-1); return(ans); } // fraction 結束 //實數階乘 private static int fraction(double y) { int ans; int newy; newy=(int) y; //換成整數 System.out.println("哪有人輸入實數的,下次請輸入整數:"+newy); ans=1; if (newy<=1) ans=1; else ans=newy*fraction(newy-1); return(ans); } // fraction 結束 }