【问题标题】:for loop calculation giving null after several loopsfor 循环计算在几个循环后给出 null
【发布时间】:2023-03-31 23:07:01
【问题描述】:

我想根据用户输入制作图表,当用户输入数字并按下按钮时,onpressed 函数进入 for 循环以计算所有图形点。但是现在 for 循环给出了错误的值,并且在一定数量的循环之后它开始在这里和那里给出空值,不确定是什么原因造成的。

这是我的 onPressed 函数:

onPressed: () {
              setState(() {
                  for (int i = 1; i <= 20; i++) {
                    for (int j = 0; j < 6; j++) {
                      value1 = 0.0;
                      print(i.toString() + ' , ' + j.toString());
                      if (j == 0) {
                        value1 = (_finalDrive *
                                (i * 10 * mphtokmph) *
                                _gear1 *
                                _coefficient) /
                            _tireDiameter;
                      } else if (j == 1) {
                        value1 = (_finalDrive *
                                (i * 10 * mphtokmph) *
                                _gear2 *
                                _coefficient) /
                            _tireDiameter;
                      } else if (j == 2) {
                        value1 = (_finalDrive *
                                (i * 10 * mphtokmph) *
                                _gear3 *
                                _coefficient) /
                            _tireDiameter;
                      } else if (j == 3) {
                        value1 = (_finalDrive *
                                (i * 10 * mphtokmph) *
                                _gear4 *
                                _coefficient) /
                            _tireDiameter;
                      } else if (j == 4) {
                        value1 = (_finalDrive *
                                (i * 10 * mphtokmph) *
                                _gear5 *
                                _coefficient) /
                            _tireDiameter;
                      } else if (j == 5) {
                        value1 = (_finalDrive *
                                (i * 10 * mphtokmph) *
                                _gear6 *
                                _coefficient) /
                            _tireDiameter;
                      }
                      values[int.parse(((j) * (i)).toStringAsFixed(0))] =
                          value1;
                    }
                  }
              });
            },

输出如下: I/flutter (21824): [47049.25, 1383.39, 2766.77, 4150.16, 5533.55, 6916.93, 8300.32, 9683.7, 11067.09, 12450.48, 13833.86, 15217.25, 16600.64, 17984.02, 19367.41, 20750.8, 22134.18, 23517.57, 24900.96, 26284.34, 27667.73, 4940.67, 10504.56, null, 11459.52, 2075.08, 12414.48, 6352.28, 13369.44, null, 14324.4, null, 15279.36, 7763.9, 16234.32, 2905.11, 17189.28, null, 18144.24, 9175.52, 19099.2, null, 9881.33, null, 5652.12, 10587.14, null, null, 11292.95, null, 4150.16, 11998.76, 6679.78, null, 12704.57, 4565.18, 7193.61, 13410.38, null, null, 14116.19, null, null, null, 8221.27, 5395.21, 4565.18, null, 8735.1, null, 5810.22, null, 9248.93, null, null, 6225.24, 9762.76, null, 5395.21, null, 10276.58, null, null, null, 5810.22, 7055.27, null, null, null, null, 7470.29, null, null, null, null, 7885.3, 6640.25, null, null, null, 8300.32, null, 7055.27, null, null, null, null, null, 7470.29, null, null, null, null, null, 7885.3, null, null, null, null, null]

【问题讨论】:

  • 不是您问题的答案,但是当您使用后缀数字命名变量时,它是代码气味。如果你有 gear[j] 而不是单独的变量,这段代码会简单得多。您甚至不需要内部开关,只需 var value= (_finalDrive * (i * 10 * mphtokmph) * gear[j] * _coefficient) / _tireDiameter; 看,更简单。

标签: for-loop null dart flutter


【解决方案1】:

数组索引的问题 代码

indice = -1 
For loop1...
  For loop2...
   indice = indice +1 
      :
      :
value[indice] = value1

【讨论】:

  • 所有变量都是双倍的,甚至不为空的值也是错误的,我在 excel 中使用了相同的方程,第一个值得到了 908.3
  • 如果有帮助,_finalDrive 的值为 4.11,mphtokmph 为 1.609344,_coefficient 为 336.13,_tireDiameter 为 31.5,_gear1 为 3.333,_gear2 为 1.96,_gear3 为 1.353,_gear4 为 1,_gear4 为 8,_gear 为 8 _gear6 为 0.588
  • 我不太懂飞镖,但是你把结果放在数组里,所以索引 i * j 中每个元素的值,正常吗?每次 j = 0,你写索引 0.. 第一个索引是 0,最后一个是 5 * 19
  • 哦,好吧,我明白你的意思,这是有道理的,但我应该如何让它在 for 循环中从元素 0 到 119
  • 我想我想通了,我会在内部循环中有一个单独的计数器,每次都会递增,我会用它作为列表的索引,谢谢
猜你喜欢
  • 1970-01-01
  • 2021-01-31
  • 2022-06-13
  • 1970-01-01
  • 2020-07-07
  • 1970-01-01
  • 1970-01-01
  • 2013-10-24
  • 1970-01-01
相关资源
最近更新 更多