Topic: 哪位大哥帮忙 多谢了 (输入整数n, 计算e=1/0!+...+1/n!))

  Print this page

1.哪位大哥帮忙 多谢了 (输入整数n, 计算e=1/0!+...+1/n!)) Copy to clipboard
Posted by: zidane2000
Posted on: 2004-09-17 00:30

编写一个Java Application程序,接受用户输入的一个整数n,计算e=1+1/1!+1/2!+1/3!+1/4!+…+1/n!,(检查1/n!<1E-5、考虑利用上一道题的代码)

2.Re:哪位大哥帮忙 多谢了 [Re: zidane2000] Copy to clipboard
Posted by: why
Posted on: 2004-09-17 02:45

考虑利用上一道题的代码?哪道题?请你为人设想一点儿,好吗?
阁下在请教他人喔。
唉──

I don't know what the requirement of 接受用户输入 is, but this is what I write for http://www.cjsdn.net/post/view?bid=1&id=110544
you may use a method for the calculation part and develop the proper "接受用户输入"

public class E {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
double delta = Double.parseDouble(args[1]);

double iFactorial = 1.0;
double sum = 1.0;
for (int i=1; i<=n; i++) {
iFactorial = iFactorial / (double)i;
if (iFactorial < delta) {
break;
} else {
sum = sum + iFactorial;
}
}
System.out.println("E = " + sum);
System.out.println("Math.E = " + Math.E);
System.out.println("Diff = " + (Math.E - sum));
}
}

3.Re:哪位大哥帮忙 多谢了 (输入整数n, 计算e=1/0!+...+1/n!)) [Re: zidane2000] Copy to clipboard
Posted by: eric_dasini
Posted on: 2004-09-17 14:29

public class test{
  public static void main(String []args){
    System.out.println(new test().operation(3));
  }
  
  double operation(int n){
    double fenmu = 1;
    double fenzi = 1;
    double sum = 1;
    
    for(int i=1; i<n+1; i++){
      for(int j=1; j<i+1; j++){
        fenmu = fenmu*j;    
      }
      
      sum = sum + fenzi/fenmu;
    }
    
    return sum;
  }
}

判断自己添吧!!!


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923