当前位置: > JAVA 阶乘 的倒数求和...
题目
JAVA 阶乘 的倒数求和
public class Jiecheng {
public static void main(String args[])
{
int n=1,fct=1;
while(fct>1e4)
{
fct*=n;
n+=1
}
Jiecheng aa=new Jiecheng();
int sum=0,i,a;
if(i=1;i

提问时间:2020-08-01

答案
1
你的程序有很多的错误
给你程序做了一下更改
一个文件里只能有一个class,你的那个class Jiecheng
{
int show(int i)
{
if(i==1)
return 1;
else
return i*show(i-1);
}
应该是个方法,不应该这么写,另外while(fct>1e4)
{
fct*=n;
n+=1
}
也不知道你要干什么,就给你注释了,代码如下
public class Jiecheng {
public static void main(String args[]) {
int n = 5,fct = 1;
// while (fct > 1e4) {
// fct *= n;
// n += 1;
// }
Jiecheng aa = new Jiecheng();
float a,sum = 0;
int i = 0;
for (i = 1; i < n + 1; i++) {
a = aa.show(i);
sum = sum + 1 / a;
}
System.out.println("e=" + sum);
}
public int show(int i) {
if (i == 1) {
return 1;
} else {
return i * show(i - 1);
}
}
}
结果如下
e=1.7166666
举一反三
已知函数f(x)=x,g(x)=alnx,a∈R.若曲线y=f(x)与曲线y=g(x)相交,且在交点处有相同的切线,求a的值和该切线方程.
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
版权所有 CopyRight © 2012-2019 超级试练试题库 All Rights Reserved.