我需要一些关于如何完全使用全局变量的指导。
我有全局变量
num-tourists
,它是有多少游客来到某个地方的数字。
然后,我让我的海龟获得最大数量的游客(15),并收取 100,000 的费用。我的每个卖家最多可以接待 15 名游客。
ask turtles with [turism = 1]
[set earnings (earnings + ((15) * 100000))]
[set num-tourists num-tourists - 15]
但是,我不知道如何告诉代码,如果少于 15 个,它可以取剩下的,然后乘以 100000
这类似于 NetLogo 库中的“走向目标示例”。从功能上讲,您想要: 计算剩余游客的数量。如果该数字大于 15,则乘以 10000 X 15。否则,乘以 10000 X(剩余 n 个游客)。这是一种方法:
globals [num-tourists total-earnings]
to setup
ca
set num-tourists 40
set total-earnings 0
print (word "There are " num-tourists " tourists")
reset-ticks
end
to go
ifelse num-tourists > 15 [
set total-earnings total-earnings + (15 * 10000)
set num-tourists num-tourists - 15
] [
set total-earnings total-earnings + (num-tourists * 10000)
set num-tourists num-tourists - num-tourists
; equivalently, set num-tourists 0
]
print (word "There are " num-tourists " tourists, and the total earnings are " total-earnings)
tick
end