題目:求1+2!+3!+…的前n項的和 – JAVA編程語言程序開發技術文章

<span style="font-size:24px;BACKGROUND-COLOR: #ff0000"><strong>歡迎大傢多提寶貴意見,我會仔細觀看的:</strong></span> 
[java]
import java.util.Scanner; 
 
/*題目:求1+2!+3!+…的前n項的和   
1.程序分析:此程序隻是把累加變成瞭累乘。*/ 
<span style="BACKGROUND-COLOR: #33ff33">public class Factorial { 
    public static void main(String[]args){ 
        int sum = 0; 
        System.out.println("請輸入多少項階乘的和:"); 
        Scanner in = new Scanner(System.in); 
        int n = in.nextInt(); 
        int an = 0; 
         
        for(int i = 1;i <= n + 1;i++){ 
            if(i == n + 1){ 
                sum += an; 
            }else if(i == n){ 
                sum += an; 
                an = 1; 
                System.out.print(i+"!"); 
                for(int j = i;j >= 1;j–){ 
                    an *=j; 
                } 
            }else if(i%10 == 0){ 
                System.out.println(); 
                sum += an; 
                an = 1; 
                System.out.print(i+"!"+"+"); 
                for(int j = i;j >= 1;j–){ 
                    an *=j; 
                } 
            }else{ 
                sum = sum + an; 
                an = 1; 
                System.out.print(i+"!"+"+"); 
                for(int j = i;j >= 1;j–){ 
                    an *=j; 
                } 
            } 
        }System.out.println(" = "+sum); 
    } 

</span> 

摘自 djw10962000的專欄

發佈留言