大家帮忙看看这个有关阶乘的题!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);
}
}
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);
}
}
解决方案 »
- 下面的程序程序哪个方法体先执行?m1还是m2?高手分析一下好吗?进来看看
- 【问题】java与orcle的关系是?若要使用orcle是不是先要学会java?
- 请问:输出1-100之内的奇数,麻烦看这个程序,为什么不对呢?
- 怎样使表头有排序功能
- 请问怎样判断一个文件是否存在?谢谢
- 在JFrame的构造函数中,JButton.addActionListener(this),this代表什么?原型可是java.awt.event.ActionListener
- 请大家推荐JAVA教材!
- 请问VisualAgeforJava怎么样?是否用的sun的JDK?与JBuilder哪个更适用与做项目?
- 线程问题
- poi读取1个excelA.xls,然后将excelA的数据赋值到excelB.xlsx中
- 还没有达到菜鸟提的问题?
- 【求助】生成XML问题?
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);
}
}
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);
}
}