C# 我试图在数组中找到最近的双精度

C# 我试图在数组中找到最近的双精度,c#,C#,我在这里尝试遵循代码: 但是失败了 与我的代码唯一不同的是,我的代码中存在MinBy is error(有红色下划线),它在Visual Studio中显示了一个错误 顺便说一句,这是我写的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.

我在这里尝试遵循代码:

但是失败了

与我的代码唯一不同的是,我的代码中存在MinBy is error(有红色下划线),它在Visual Studio中显示了一个错误

顺便说一句,这是我写的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            double[] array = new double[5] { 0.25, 0.4, 0.5, 0.6, 0.7 };

            double TargetNumber = Double.Parse(textBox1.Text);

            var nearest = array.MinBy(x => Math.Abs((long)x - targetNumber));
            label1.Text = nearest.ToString();
        }
    }
}
我是新来的。对不起,有个问题。
提前感谢。

有时实现一个库只使用一种方法听起来有点像是过激(即使
MoreLINQ
是一个了不起的库)。。。如果这是一个很好的解决方案,那么这段代码应该在不使用外部库的情况下为您提供相同的结果:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Double[] array = new Double[] { 0.25, 0.4, 0.5, 0.6, 0.7 };
            Double targ = Double.Parse(textBox1.Text);

            Double[] arraySorted = array.OrderBy(x => Math.Abs(x - targ)).ToArray();
            Int32 idx = Array.IndexOf(array,arraySorted.First());

            label1.Text = idx.ToString();

            Double arrayValue = array[idx];
            Int32 idxLower = 0;
            Int32 idxUpper = 0;

            if (targ == arrayValue)
                idxLower = idxUpper = idx;
            else
            {
                if (targ > arrayValue)
                {
                    idxLower = idx;
                    idxUpper = idx + 1;
                }
                else
                {
                    idxLower = idx - 1;
                    idxUpper = idx;
                }
            }

            label2.Text = idxLower.ToString();
            label3.Text = idxUpper.ToString();
        }
    }
}

你把事情搞得太复杂了。这是您的“面包和黄油”助手方法,它从数组
a
中查找最接近传递值
d

private static double? FindNearestValue(IEnumerable<double> arr, double d)
{
    var minDist = double.MaxValue;
    double? nearestValue = null;

    foreach (var x in arr)
    {
        var dist = Math.Abs(x - d);
        if (dist < minDist)
        {
            minDist = dist;
            nearestValue = x;
        }
    }
    return nearestValue;
}

对于小数组,线性搜索的速度与二进制搜索相当。如果实现二进制搜索对您来说是个问题,并且您是
c#
新手(不习惯LINQ电源),那么好的ol
foreach
现在是您的朋友。

您需要添加
MoreLINQ
Nuget包——才能访问
MinBy
。我已经更新了你链接到的帖子,在那里添加了那个URL。另见。同时删除
(long)
您可以使用NuGet“System.Interactive”获取
MinBy
操作符。@mjwills,我更新代码并安装NuGet包。它解决了MinBy问题。但当我运行程序,在文本框中输入一个值(0.3)并点击按钮时,它并没有给出最接近的数字,而是类似于“System.Collection.Generic.List'1[System.Double]”的数字。我不知道是谁投了反对票。但是对于代码来说,它更简单,但是我能得到,最近的上/最近的下吗?嗯,你是什么意思?你的意思是处理负数?正如我在给出的代码中看到的,它将给出最接近的值。例如,如果TargetNumber=0.43,它将选择0.4作为最接近的数字,但如果大于0.45,它将选择0.5作为最接近的数字。我可以使输出始终选择上/下值吗。例如,如果我输入0.42或0.48,输出将始终为0.5,它将选择最接近的上限值。使用上限和下限,如果我们在左端以下或右端以上,函数将返回什么?好的,我添加了上限和下限。但是,除非您不告诉我如何处理溢出数组维度的下界和上界索引,否则代码无法进一步改进。如果您的问题满足您的要求,请接受,谢谢!
private void button1_Click(object sender, EventArgs e)
{
    double[] array = new double[5] { 0.25, 0.4, 0.5, 0.6, 0.7 };

    double TargetNumber = Double.Parse(textBox1.Text);
    var nearest = FindNearestValue(array, TargetNumber);

    label1.Text = nearest.ToString(); // nulls are printed as empty string
}