我想先写一个事实,即我从未真正学过如何使用Netlogo正确编码,我所做的一切都是经过反复试验,当我遇到问题时在这里提问。所以,如果这是一个愚蠢的问题,我道歉!
我目前正在制作一个模型,其中每个刻度等于一天。我有一个特定的动作,我希望在特定的刻度(当刻度= 60,425,790,1155,1520,1855等)时发生。
我试过了:
if ticks = [60 425 790 1155 1520 1885]
[
create-hatchlings Hatchling-Release
[ set color 57
set size 1.5
move-to one-of patches with [ pcolor = cyan ]
set birth-tick -60]
create-m-hatchlings Hatchling-Release
[ set color 107
set size 1.5
move-to one-of patches with [ pcolor = cyan ]
set m-birth-tick -60]
]
使用此代码,没有任何反应。我能够在正确的时间让事件发生的唯一方法是为每个数字分别编写,如下所示:
if ticks = 60
[
create-hatchlings Hatchling-Release
[ set color 57
set size 1.5
move-to one-of patches with [ pcolor = cyan ]
set birth-tick -60]
create-m-hatchlings Hatchling-Release
[ set color 107
set size 1.5
move-to one-of patches with [ pcolor = cyan ]
set m-birth-tick -60]
]
if ticks = 425
[
create-hatchlings Hatchling-Release
[ set color 57
set size 1.5
move-to one-of patches with [ pcolor = cyan ]
set birth-tick -60]
create-m-hatchlings Hatchling-Release
[ set color 107
set size 1.5
move-to one-of patches with [ pcolor = cyan ]
set m-birth-tick -60]
]
但我最终必须做数百次。有没有更好的方法来写这个?
答案 0 :(得分:3)
使用member?
检查列表中是否显示刻度值:
if member? ticks [60 425 790 1155 1520 1885]
[
; do something
]
另一方面 - 如果你想在每年的第60天做一些事情,请使用modulo(mod
):
if ticks mod 365 = 60
[
; do something
]
所以你不需要这份清单。