编写一个Java程序,“求1+2!+3!+.......+n!的和”

2024-11-25 19:08:02
推荐回答(1个)
回答1:

public
class
FactorialPlus
{
int
f(int
n)
{
int
t
=
0;
int
s
=
1;
for(int
i
=
1;i
<=
n;i
)
{
s
=
s
*
i;
t
=
t
s;
}
return
t;
}
public
static
void
main
(String
[]
args)
{
int
m
=
20;//改变这个m的值即可改变输出结果
FactorialPlus
p
=
new
FactorialPlus();
if(m
>=
0)
{
System.out.println(p.f(m));
}
else
{
System.out.println("请输入一个大于0的数");
}
}
}