且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将双打列表中的所有元素相乘?

更新时间:2023-02-10 15:34:16

要修复循环,请以1.0开头,并将列表中的每个项目相乘,如下所示:

To fix your loop, start with 1.0 and multiply each item in the list, like this:

double r = 1.0;
for(int i = 0; i < mult.Count; i++)
{
    r = r * mult[i]; // or equivalently r *= mult[i];
}

但是为了简单起见,您可以在 Aggregate中使用一点Linq 扩展方法:

But for simplicity, you could use a little Linq with the Aggregate extension method:

double r = mult.Aggregate((a, x) => a * x);