如何添加阶乘值的结果?
我这里有逻辑问题。我想添加阶乘值的结果,但我不知道如何添加它们。这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Task_8_Set_III
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 7; i++)
{
double c = i / fact(i);
Console.WriteLine("Factorial is : " + c);
Console.ReadLine();
Console.WriteLine("By Adding.. will give " +);
}
}
static double fact(double value)
{
if (value ==1)
{
return 1;
}
else
{
return (value * (fact(value - 1)));
}
}
}
}
I'm having a logic problem here. I want to add the result of the factorial values but I'm not sure how to add them. Here's my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Task_8_Set_III
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 7; i++)
{
double c = i / fact(i);
Console.WriteLine("Factorial is : " + c);
Console.ReadLine();
Console.WriteLine("By Adding.. will give " +);
}
}
static double fact(double value)
{
if (value ==1)
{
return 1;
}
else
{
return (value * (fact(value - 1)));
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不确定这是否是您的意思,但如果对于 N 的阶乘,您希望所有阶乘的总和达到该值,这就是您的做法。
Not sure if this is what you meant but if for factorial of N you want to have the sum of all factorials up to that value this is how you do it.
您需要添加一个总计变量来跟踪总和。
You need to add a total variable to keep track of the sum.
缺乏完全理解你想要做什么,这里有两件事......
i = i + 1
表示“i 的新值是 i 的旧值加一”{ }
,即您将需要一个位于 foreach 大括号之外的变量,以便“记住”上一次迭代的内容。short of totally understanding what you want to do exactly, here's two things...
i = i + 1
Saying "the new value of i is the old value of i plus one"{ }
, that is you will need a variable that is outside the braces of the foreach in order to "remember" stuff of the previous iteration.