int[] myArray = new int[]{-8, -17, 12, 8, 16, -3, 7, 3};
查找最接近的阵列中的所有数值为0
目前使用的网站上找到了另一个代码,但它只是告诉我1值。
int nearest = myArray.Select(p => new {Value = p, Difference = Math.Abs(p - searchValue)}).OrderBy(p => p.Difference).First().Value;
在当前情况下,-3和3都最接近0,但由于-3最先出现,它只输出-3。
是否有另一种方式来做到这一点,它发现所有的价值,而不是只是First()?
你的调用.First()。所以你只会得到1结果。 – Botonomous
并将它分配给一个'int'以便... – djv