对C#中列表中的每个输入执行计算

时间:2019-02-26 02:36:09

标签: c# arraylist

我的程序有大约4个输入,分别输入到每个earinglist和distanceList。我如何执行对BearingList和distanceList的所有输入的计算,然后添加到新list(latList)?在低于最终结果的情况下使用建议方法,但报价中的值全部错误,是我使用计算器手动计算的正确值。是我循环造成的吗?

Latitude :145.813468086875    (wrong)  
Latitude :-375.892492736719
Latitude :541.064120980176
Latitude :200.457427906608
Departure :244.990698852993
Departure :-480.992761280433
Departure :475.753137023761
Departure :32.0284185851739
  

纬度出发(正确)

     

+255.88 +125.72

     

-153.70 +590.78

     

-694.28 -192.54

     

+202.91 -6.02

     

+388.48 -517.41

        List<double>bearingList = new List<double>();
        List<double> distanceList = new List<double>();

        Console.WriteLine("Insert bearing angle A:");
        double bearA = 26.16667;
        bearingList.Add(bearA);

        Console.WriteLine("Insert distance travel from A:");
        double distA = 285.10;
        distanceList.Add(distA);

        Console.WriteLine("Insert bearing angle B:");
        double bearB = 104.58;
        bearingList.Add(bearB);

        Console.WriteLine("Insert distance travel from B:");
        double distB = 610.45;
        distanceList.Add(distB);

        Console.WriteLine("Insert bearing angle C:");
        double bearC = 195.5;
        bearingList.Add(bearC);

        Console.WriteLine("Insert distance travel from C:");
        double distC = 720.48;
        distanceList.Add(distC);

        Console.WriteLine("Insert bearing angle D:");
        double bearD = 358.3;
        bearingList.Add(bearD);

        Console.WriteLine("Insert distance travel from D:");
        double distD = 203.0;
        distanceList.Add(distD);

        List<double> latList = new List<double>();


        for (int i = 0; i < distanceList.Count; i++)
        {
            var result1 = distanceList[i] * Math.Cos(bearingList[i]);
            latList.Add(result1);
            Console.WriteLine("Latitude :" + latList[i]);
        }

        //double calLat = distanceList. * Math.Cos(bearingList[0]);
        //latList.Add(calLat);
        List<double> departList = new List<double>();

        for (int i = 0; i < distanceList.Count; i++)
        {
            var result2 = distanceList[i] * Math.Sin(bearingList[i]);
            departList.Add(result2);
            Console.WriteLine("Departure :" + departList[i]);
        }

4 个答案:

答案 0 :(得分:6)

您可以列出一个名为Tuple的列表,然后使用 Linq

var list = new List<(double distance, double bearing)>();
list.Add((234,456));

var results = list.Select(x => x.distance * Math.Cos(x.bearing));

或者您可以Zip与Linq一起

List<double>bearingList = new List<double>();
List<double> distanceList = new List<double>();

var results = distanceList.Zip(bearingList, (d, b) => d * Math.Cos(b));

或者是老式的for循环

for (int i = 0; i < distanceList.Count; i++)
{
   // do something
   var result = distance[i] * Math.Cos(bearing[i]);
   Results.Add(result);
}

答案 1 :(得分:1)

您可以使用“循环”来计算两个列表的所有元素,并将它们添加到新列表中。如果您还没有学会“循环”的概念,则可以一一计算。

答案 2 :(得分:1)

尝试添加更多选项,这是另一个使用Enumerable.Range的解决方案

List<double> bearingList = new List<double>();
List<double> distanceList = new List<double>();

var results = Enumerable.Range(0, bearingList.Count).Select(n => distanceList[n] + Math.Cos(bearingList[n])).ToList();

答案 3 :(得分:0)

这是我使用循环方法进行列表后的代码。我上面的编辑代码忘了将角度转换为弧度以进行支撑。

<script type="module" src="/js/api-service.js"></script>