繁体   English   中英

如何找到数组中的值?

[英]How to find the value in an array?

如何在数组中找到值,下面我给出了到目前为止我所做的代码。

 double finVal = 300;
    string[] dbvals ={"8^10","16^18","24^34","32^63","40^116","48^215","56^397","64^733","72^1354","80^2500"};
    
    for (int l = 0; l < dbvals.Length() ; l++)
    {
     string[] lc = dbvals[l].Split('^');
      double xl = Convert.ToDouble(lc[0]);
      double yl = Convert.ToDouble(lc[1]);
            if(y1>finVal)
            -- i want to find the value between 48 to 56
            --how to do it
    }

以下应该得到你所追求的, dotnetfiddle 示例

private static void BetweenTwoNumbers()
{
    double firstAssertion = 300; 

    string[] doubleValues =
    {
        "8^10", "16^18", "24^34", 
        "32^63", "40^116", "48^215", 
        "56^397", "64^733", "72^1354", 
        "80^2500"
    };

    for (int index = 0; index < doubleValues.Length; index++)
    {
        var parts = doubleValues[index].Split('^');
        var part1 = Convert.ToDouble(parts[0]);
        var part2 = Convert.ToDouble(parts[1]);


        if (part2 > firstAssertion && part1 is >= 48 and <= 56)
        {
            Console.WriteLine($"[{part1}], [{part2}]");
        }

    }

}

验证代码:

我不希望答案是 48 或 56。答案在他们之间。 验证以下代码,这解决了我的目的。

链接: https://dotnetfiddle.net/M2TDWT

using System;

public class Program
{
    public static void Main()
    {
        double finVal = 300;
        double oldval1 = 0;
        double oldval2 = 0;
        double ansval = 0;
        string[] dbvals = {"8^10", "16^18", "24^34", "32^63", "40^116", "48^215", "56^397", "64^733", "72^1354", "80^2500"};
        for (int index = 0; index < dbvals.Length; index++)
        {
            var parts = dbvals[index].Split('^');
            var part1 = Convert.ToDouble(parts[0]);
            var part2 = Convert.ToDouble(parts[1]);
            if (part2 > finVal)
            {
                ansval= Math.Round( (Convert.ToDouble(8)/Convert.ToDouble(part2-oldval2))*(finVal-oldval2));
                Console.WriteLine("The Given Find Value = "+ finVal);
                Console.WriteLine("The current first Array Element = "+ part1);
                Console.WriteLine("The current second Array Element = "+ part2);
                Console.WriteLine("The previous first Array Element = "+ oldval1);
                Console.WriteLine("The previous second Array Element = "+ oldval2);
                Console.WriteLine("The Difference between first Array Element = "+ (part1-oldval1));
                Console.WriteLine("The Difference between second Array Element = "+ (part2-oldval2));
                Console.WriteLine("The Difference between findvalue and previous second array = "+ (finVal-oldval2));
                Console.WriteLine("The Calculation : (firstarraydiff)/(secondarraydiff)*(givenfindval-previoussecondarrayvalue)");
                Console.WriteLine("The result (previoussecondarrayvalue+calculatedvalue) = " + (oldval1+ansval));
                Console.WriteLine("The Finded Value "+ (ansval));
                break;
            }
            else
            {
                oldval1 = part1;
                oldval2 = part2;
                
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM