代码不响应属性的更改

时间:2017-01-10 08:45:56

标签: netlogo

所以我有aNum作为补丁自己的属性,在我的代码中我有 ask patch 0 -5 [set aNum (aNum - 1)]但补丁只是将其aNum更改为-1。

如何确保它先前分配了aNum并从中减去了1?

谢谢!

1 个答案:

答案 0 :(得分:0)

它符合您的期望。所以问题是,为什么你看不到你的期望。找到答案就叫做"调试"。一种简单的调试方法是使用print语句来帮助您检查程序的逻辑。 (另一种方法是使用error标记意外的程序状态。)例如,

globals [testPatch]
patches-own [aNum]

to decrementANum [#patch]
  ask #patch [set aNum (aNum - 1)]
end

to test
  set testPatch patch 0 -5
  repeat 5 [
    let _before [aNum] of testPatch
    decrementANum testPatch
    print (word "before: " _before "; after: " [aNum] of testPatch)
    ]
end

一般来说,编写这样一个最小的自包含示例会提出一个更好的问题。最有可能的是,在尝试生成示例时,您会发现错误。

相关问题