【问题标题】:a for loop with a list c#带有列表的 for 循环 c#
【发布时间】:2020-11-04 22:59:20
【问题描述】:

我正在尝试将列表应用于 for 循环。 sumlist[sz] 返回许多数字,if (sumOfVolumes4 >= sumlist[sz]) 应该测试每个数字,但只测试最后一个数字而不测试其他数字。 foundIndex 应该返回多个答案,但只返回一个。

for (double d = 1.61D; d < 4.24; d+= 0.01D) {
    List<double> sumlist = new List<double>() {d};
                       
    for(int sz = 0; sz < sumlist.Count; sz++) {
        sumlist[sz] = (volumeValue /= sumlist[sz]);
                                    
        for(int barIndex = index; barIndex >= ChartBars.FromIndex; barIndex--) {
            sumOfVolumes4 +=  Bars.GetVolume(barIndex);
            if (sumOfVolumes4 >= sumlist[sz]) {
                foundIndex = barIndex;
                break;
            }
        }
    }
}
        

【问题讨论】:

  • 我不懂打印(sumlist);我得到的值与 1.61 和 4.24 之间的差值一样多。
  • 您可能需要更好地解释一下。我不太明白这段代码背后的目标。必须有一种不同于 3 个 for 循环的方式来进行。也许是一些 Linq 或 Foreach?
  • 此外,sumlist 始终包含 1 个值,因为您在循环开始时分配了它。所以第二个循环只迭代一次。一个想法是最终执行第一个 forLoop 以将所有值添加到 sumList。然后在 sumlist 上进行 foreach
  • 第一个 for 循环枚举 1.61 到 4.24,第二个应用列表将 5000 除以 1.61、1.62 等直到 4.24 在第三个 for 循环中,我有 5000/1.61 = 3105、5000/1.62, 3086 等,每个答案都经过测试,以使用 if 语句找到 barIndex 等于 3105、3086 等。foundIndex 应枚举所有满足条件的 barIndex。

标签: c# list loops for-loop


【解决方案1】:

从提供的代码看来,似乎不需要第二个 for 循环,因为 sumlist 总是被新创建,并且只包含一个在创建时分配的条目。其实sumlist本身不需要列表

所以您提供的代码归结为:

  for (double d = 1.61D; d < 4.24; d+= 0.01D) 
  {
     double sumList = (volumeValue /= d);
     for(int barIndex = index; barIndex >= ChartBars.FromIndex; barIndex--) 
     {
        sumOfVolumes4 +=  Bars.GetVolume(barIndex);
        if (sumOfVolumes4 >= sumList) 
        {
           foundIndex = barIndex;
           break;
        }
     }
  }

您的foundIndex 变量也会在每次增加最外层for 循环索引时更新。最好保存一个固定索引列表来存储foundIndex 结果。

      List<int> foundIndices = new List<int>();
      for (double d = 1.61D; d < 4.24; d+= 0.01D) 
      {
         double sumList = (volumeValue /= d);
         for(int barIndex = index; barIndex >= ChartBars.FromIndex; barIndex--) 
         {
            sumOfVolumes4 +=  Bars.GetVolume(barIndex);
            if (sumOfVolumes4 >= sumList) 
            {
               //foundIndex = barIndex;
               foundIndices.Add(barIndex);
               break;
            }
         }
      }

       foreach(var foundIndex in foundIndices)
       {
          Print(foundIndex);
       }

【讨论】:

  • 通过消除 volumeValue 您将 1.61 、 1.62、 1.63 和 4.24 与 sumOfVolumes4 进行比较,这是一个更大的数字。 sumOfVolumes4 返回大于 1.61 的每根柱的交易量。 sumOfVolumes4 = 音量,必须与 sumlist[sz] 进行比较,后者也返回音量列表。
  • @FrankDuc 感谢您的评论。删除第二个循环时我完全错过了它。我已经编辑了我的答案
  • 如果我打印(barIndex);它一遍又一遍地返回相同的数字。如果我打印(找到索引);在外面我得到 sumlistSystem.Collections.Generic.List`1[System.Int32] 在括号内它什么都不返回,一部分正在工作 double sumList = (volumeValue /= d);返回正确答案,但与 sumOfVolumes4 相比,它未通过测试。请记住 sumOfVolumes4 累积每个 barIndex 500、1000、1500 等中的交易量,当它满足 sumList 中的一个值时,它应该返回累积交易量的 barIndex,然后达到下一个。
  • @FrankDuc 您需要使用循环单独打印每个找到的索引值。我已经编辑了我的答案供您参考
  • 就像我说的那样,它仍然多次返回 bar 1227,这是 for 循环(索引)的开始,它从 bar 1227(索引)开始,直到没有 bar 到 for 循环。它相当于 Print(barIndex);在第二个 for 循环之后。谢谢你的努力。
猜你喜欢
  • 2018-12-27
  • 2017-02-18
  • 2019-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
相关资源
最近更新 更多