编写Java Application 程序,按下列公式计算e的值,
e=1+1/1!+1/2!+1/3!+.........
编写Java Application 程序,按下列公式计算e的值,
e=1+1/1!+1/2!+1/3!+.........
package com.sambow.test;
public class Demo {
public double factorial(int n){
double result = 1;
for(int i=1;i<=n;i++){
result *= i;
}
return result;
}
public double computingFormula(int n){
double result = 1;
System.out.print(1);
for(int i=1;i<=n;i++){
System.out.print("+"+"1/"+i+"!");
result += 1/this.factorial(i);
}
System.out.println();
return result;
}
public static void main(String[] args) {
Demo demo = new Demo();
double e = demo.computingFormula(10);//测试结果
System.out.println(e);
}
}
//测试结果: