更新时间: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);