【问题标题】:Creating a Turtle and placing it in a patch that isnt Black and that doesnt have another turtle in place创建一只乌龟并将其放置在一个不是黑色且没有另一只乌龟的补丁中
【发布时间】:2021-02-20 15:05:37
【问题描述】:

我正在尝试创建海龟并将它们随机放置在补丁上,但如果补丁颜色为黑色,我无法放置它们。老实说,我被卡住了,甚至无法思考……这就是我所拥有的。

create-mice N-mice
  [
    set shape "mouse side"
    set color 4
    setxy random-pxcor random-pycor
  ]
  
  ask turtles [
    while pcolor = black [
      setxy random-pxcor random-pycor
    ]
  ]

它给我一个错误说“WHILE 期望这个输入是一个 TRUE/FALSE 块,但得到一个 TRUE/FALSE 代替”

【问题讨论】:

    标签: netlogo agent


    【解决方案1】:

    这有一个内部原因(布尔值和返回布尔值的报告器之间的区别,或类似的东西),它也总是在欺骗我。

    当你在做while时,条件在[]中。注意,当你在做if时,条件不在[]中!试试这个:

    ask turtles [
        while [pcolor = black] [               ; changed the [ ] on this line
          setxy random-pxcor random-pycor
        ]
      ]
    

    【讨论】:

    • 正如向我解释的那样,使用if,你有if pcolor = black [ ... ],它只被评估一次,[]while [pcolor = black] [ ... ] 中的条件周围是为了强调条件是一遍又一遍地评估,每次都可以给出不同的结果。但是,是的,它也让我感到困惑!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 2022-11-19
    相关资源
    最近更新 更多