C# 如何获取列表的乘积?

C# 如何获取列表的乘积?,c#,factorial,C#,Factorial,我正试图用C#制作一个阶乘计算器,但在将所有数字收集到一个列表中之后,我很难计算出它们的乘积 List<int> myList = new List<int>(); Console.WriteLine("My Job is to take the factorial of the number you give"); Console.WriteLine("What is the number?"); str

我正试图用C#制作一个阶乘计算器,但在将所有数字收集到一个列表中之后,我很难计算出它们的乘积

        List<int> myList = new List<int>();

        Console.WriteLine("My Job is to take the factorial of the number you give");
        Console.WriteLine("What is the number?");
        string A = Console.ReadLine();
        int C = Convert.ToInt32(A);
        T:
        myList.Add(C);
        C--;

        if (C == 0) goto End;
        goto T;
    End:
        // This is where the problem is,
        // i don't know of a way to take to product of the list "myList"
        //Any Ideas?
        int total = myList.product();
        Console.WriteLine(" = {0}", total);
        Console.ReadLine();
List myList=new List();
WriteLine(“我的工作是取你给出的数字的阶乘”);
控制台。WriteLine(“号码是多少?”);
字符串A=Console.ReadLine();
int C=转换为32(A);
T:
添加(C);
C--;
如果(C==0),则转到结束;
转到T;
完:
//这就是问题所在,
//我不知道有什么方法可以得到“myList”列表中的产品
//有什么想法吗?
int total=myList.product();
Console.WriteLine(“={0}”,总计);
Console.ReadLine();

除非你有什么需要,否则将所有数字添加到列表中似乎没有多大好处

作为一种替代方案,类似的方法应该可以奏效:

// set product to the number, then multiply it by every number down to 1.
private int GetFactorial(int number)
{
    int product = number; 
    for (var num = number - 1; num > 0; num--) 
    {
        product *= num;
    }
    return product;
}

进行阶乘运算不需要列表:

Console.WriteLine("My Job is to take the factorial of the number you give");
Console.WriteLine("What is the number?");
int c = Convert.ToInt32(Console.ReadLine());
int total = 1;

for (int i = 2; i < c; i++)
{
    total *= i;
}

Console.WriteLine(total.ToString());
Console.ReadLine();
Console.WriteLine(“我的工作是取你给出的数字的阶乘”);
控制台。WriteLine(“号码是多少?”);
int c=Convert.ToInt32(Console.ReadLine());
整数合计=1;
for(int i=2;i这是如何进行乘法运算的,但不需要阶乘,也可以搜索循环,并考虑避免<代码> Goto 。使用循环而不是<代码> Goto 为什么在一开始就将输入添加到列表中?还要避免使用
goto
I诚实地说,我甚至不知道c#had goto