2008年8月29日 星期五

計算1+1/1+1/2!+1/3!+1/4!

code
package javahomework;

import javax.swing.JOptionPane;

public class HW13 {
 static double Count(int n) {
  if (n == 0 || n == 1)
   return 1;
  else
   return n * (Count(n - 1));
 }

 public static void main(String arg[]) {
  int input;
  float ans = 0;
  input = Integer.parseInt(JOptionPane.showInputDialog(null,
    "How much do you want to count?"));
  for (int i = 0; i < input; i++)
   ans += 1 / (Count(i));
  ans -= (ans % 0.000001);
  JOptionPane.showMessageDialog(null, "Number is " + ans);
 }
}

沒有留言: