NetLogo - 所有海龟的总和列表
NetLogo - Sum lists of all turtles
我在对所有海龟的列表求和时遇到问题,我不确定哪里出错了。
例如,在下面的代码中,我想显示所有海龟中苹果的总数。每次我 运行 我得到的代码都是 0,而理论上它应该是 100。我不确定错误是什么。如有任何帮助,我们将不胜感激!
turtles-own [ apples ]
to test
clear-all
create-turtles 5 [
set apples []
set apples lput 20 apples
]
show sum [apples] of turtles
end
下面的代码和测试应该对所有海龟列表中的所有数字求和。
turtles-own [ apples ]
to test
clear-all
create-turtles 5 [
set apples []
set apples lput 20 apples
set apples lput 10 apples
]
ask one-of turtles [set apples lput 30 apples]
show sum map sum [apples] of turtles ; here's the key command
end
map sum [apples] of turtles
对每只海龟列表中的数字求和并将这些和放在一个列表中,然后第一个 sum
将它们全部加起来。我让一只乌龟的列表比其他的长,只是为了确保所有列表的长度不需要相同。
希望这对您有所帮助,
查尔斯
我在对所有海龟的列表求和时遇到问题,我不确定哪里出错了。
例如,在下面的代码中,我想显示所有海龟中苹果的总数。每次我 运行 我得到的代码都是 0,而理论上它应该是 100。我不确定错误是什么。如有任何帮助,我们将不胜感激!
turtles-own [ apples ]
to test
clear-all
create-turtles 5 [
set apples []
set apples lput 20 apples
]
show sum [apples] of turtles
end
下面的代码和测试应该对所有海龟列表中的所有数字求和。
turtles-own [ apples ]
to test
clear-all
create-turtles 5 [
set apples []
set apples lput 20 apples
set apples lput 10 apples
]
ask one-of turtles [set apples lput 30 apples]
show sum map sum [apples] of turtles ; here's the key command
end
map sum [apples] of turtles
对每只海龟列表中的数字求和并将这些和放在一个列表中,然后第一个 sum
将它们全部加起来。我让一只乌龟的列表比其他的长,只是为了确保所有列表的长度不需要相同。
希望这对您有所帮助, 查尔斯