大家帮忙看看这个有关阶乘的题!e=1+(1/1!)+(1/2!)+(1/3!)+......下面是我编的,可结果总是得1.000000,不知道哪里出了问题后面的小数位不能精确算出来import java.text.DecimalFormat;
import javax.swing.JOptionPane;
class Commantoe
{
public static void main(String[] args) 
{
int n;
int m = 1;
String theNumber;
double sum = 0; theNumber = JOptionPane.showInputDialog("enter an integer:"); n = Integer.parseInt(theNumber); DecimalFormat twoDigits = new DecimalFormat("0.00000"); while(n>0)
{
m = m*n;
    n--;
}
while(n >= 1)
{
sum = sum + (double) 1/(double)m ;
n--;
}
sum = sum + 1; JOptionPane.showMessageDialog(null, "e = " + twoDigits.format(sum), "result", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}

解决方案 »

  1.   

    import java.text.DecimalFormat;
    import javax.swing.JOptionPane;
    class Commantoe
    {
    public static void main(String[] args) 
    {
    int n = 1;
    int m = 1;
    double sum = 1;
    double temp = 1;
    String theNumber;theNumber = JOptionPane.showInputDialog("enter an integer:");
    n = Integer.parseInt(theNumber);
    DecimalFormat twoDigits = new DecimalFormat("0.00000");
    for(int i = 1; i <= n; i++)
    {
    temp = temp * i;
    sum = sum + (double) (1 / temp);
    }
    JOptionPane.showMessageDialog(null, "e = " + twoDigits.format(sum), "result", JOptionPane.INFORMATION_MESSAGE);
    System.exit(0);
    }
    }
      

  2.   

    //e=1+(1/1!)+(1/2!)+(1/3!)+......
    import java.text.DecimalFormat;
    import javax.swing.JOptionPane;
    class Commantoe
    {
    public static void main(String[] args) 
    {int i=1;
    int n;
    double m = 1;
    String theNumber;
    double sum = 0;theNumber = JOptionPane.showInputDialog("enter an integer:");n = Integer.parseInt(theNumber);//n保留展开式的阶数DecimalFormat twoDigits = new DecimalFormat("0.000000000");while(i<=n)
    {
    sum = sum +  1/m ;
    m=m*i;
    i++;
    }
    sum = sum + 1;JOptionPane.showMessageDialog(null, "e = " + twoDigits.format(sum), "result", JOptionPane.INFORMATION_MESSAGE);
    System.exit(0);
    }
    }