编写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);
}
}
//测试结果:
public static void main(String [] args){
int k,i;
double e=0.0;
long j;
for(k=1;k<??;k++) //控制计算项数
{ j=1; //计算阶乘
for(i=1;j<=j;i++) j=j*i;
j++;
e=e+1/double(j);
}
system.out.println(e);
}