【发布时间】:2017-02-16 03:44:54
【问题描述】:
以下过程旨在从众多海龟中选择一个“赞助人”并将其颜色更改为白色。选择是基于海龟的财富的概率。
运行此代码,特别是 item i ranking-in-radius 行我收到类似这样的错误消息:“cannot find element 43 in list...(shows list of turtles' wealth)... which is only of length 43.”我知道我收到此错误是因为列表从 0 开始,因此 43 个元素的列表从 0 开始到 42,但我不明白为什么我写的代码不正确。我的i 从0 开始,如果i 不是< 而不是查询的海龟数量(即应该停止在42),则程序停止。任何人都可以提出解决方案吗?谢谢!
to choose-patron
let i 0
let patrons-in-radius count turtles in-radius radius with [wealth >= 80]
let ranking-in-radius sort-by > [wealth] of turtles in-radius radius with [wealth >= 80]
let total-wealth-in-radius sum [wealth] of turtles in-radius radius with [wealth >= 80]
while [i < (patrons-in-radius)][
ask turtles in-radius radius with [wealth >= 80] [
if [wealth] of self = item i ranking-in-radius [
if random 100 < ((wealth / total-wealth-in-radius) * 100) [
set color white
if any? turtles in-radius radius with [color = white] [stop]
]
set i (i + 1)
]
]
]
end
【问题讨论】:
标签: netlogo