題目:利用遞歸方法求5!。 – JAVA編程語言程序開發技術文章

import java.util.Scanner; 
<span style="font-size:24px;BACKGROUND-COLOR: #ff0000"><strong>//歡迎大傢多提寶貴意見;我會仔細學習的;</strong></span> 
/*
題目:利用遞歸方法求5!。   
1.程序分析:遞歸公式:fn=fn_1*4!*/ 
<span style="BACKGROUND-COLOR: #66ff99">public class Recursion1 { 
    public static int DoFactorial(int n){ 
        System.out.print(n+"! = "); 
        int sum =1; 
        if(n <= 0){ 
            System.out.println("輸入錯誤,必須大於0;/n請重新輸入"); 
        }else{ 
            for(int i = n;i > 0;i–){ 
                if(i == 1){ 
                    sum *=i; 
                    System.out.print(i+"="); 
                }else{ 
                    sum *=i; 
                    System.out.print(i+"*"); 
                } 
            } 
        } 
        return sum; 
    } 
    public static void main(String[]args){ 
        System.out.println("請輸入您要求階乘的數字n="); 
        Scanner in = new Scanner(System.in); 
        int m = in.nextInt(); 
        System.out.println(DoFactorial(m)); 
        //Recursion1 r1 = new Recursion1(); 
        //r1.DoFactorial(m); 
    } 

</span> 

摘自 djw10962000的專欄

發佈留言