看不懂括号

问题描述 投票:0回答:1

我是 NetLogo 新手,我不太理解括号,NetLogo 要么说我缺少括号,要么需要括号。每当我尝试添加或删除括号时,它只会说另一个括号已损坏。单纯的代码是行不通的。有人帮忙吗!

globals [
  initial-trees  
  burned-trees ]    

breed [fires fire]      

to setup
  clear-all
  set-default-shape turtles "square" 
  ask patches with [(random-float 100) < density]
    [ set pcolor green ] 
  [ plants ]
  ask patches with [pxcor = min-pxcor]
    [ ignite ]
  set initial-trees count patches with [pcolor = green]
  set burned-trees 0
  reset-ticks
end

to go 
  if not any? turtles 
    [ stop ]
  ask fires
    [ ask neighbors4 with [pcolor = green]
     if random 100 < 50 [ignite]
       [ ask neighbors4 with [pcolor = blue]
           [ ask neighbors4 with [pcolor = grey]
         if random 100 < 25 [ignite]
  tick
end

to ignite 
  sprout-fires 1
    [ set color yellow ]
  set pcolor black
  set burned-trees burned-trees + 1
end
  
to plants
  [ ask neighbors4 with pcolor = black
    set pcolor blue ]
    [ ask neighbors4 with pcolor = blue
      set pcolor grey ]
netlogo brackets
1个回答
0
投票

NetLogo 中的括号可以表示一些不同的含义:

  • 文字值列表:
    let l [10 20 30 40 50]
    -
    l
    将有一个包含这五个数字的列表
  • 命令序列,有时称为命令块:
    ask turtles [ forward 1 set pcolor red ]
    - 每只乌龟将执行两个命令
  • 可以多次求值的条件表达式:
    let reds turtles with [color = red]
    while [count turtles < 100] [ ... ]
  • 作为全局、扩展、品种和品种自有声明的一部分:
    extensions [nw palette]
    globals [g1 g2 g3]
    breed [frogs frog]
  • 匿名任务:`let t [ [x y z] -> (x + y) * z ]

熟悉这些不同的用法有助于尝试了解何时需要使用括号。

对于您的实际代码,我建议将内容写出来以确保它始终有效,并经常编译,这样当您出现不匹配时,就不难看出原因。如果您确实有一大堆需要排除故障的代码,您也可以反转此过程,方法是注释掉代码,直到找到可以工作的代码,然后慢慢添加回去。您可以通过选择代码行并点击来快速完成此操作

Ctrl-;
注释/取消注释它们,或右键单击并在上下文菜单中选择选项。

我注释掉了

go
ignite
plants
程序,但
setup
仍然出错:

globals [
  initial-trees  
  burned-trees ]    

breed [fires fire]      

to setup
  clear-all
  set-default-shape turtles "square" 
  ask patches with [(random-float 100) < density]
    [ set pcolor green ] 
  [ plants ] ; error on this line - "Expected command."
  ask patches with [pxcor = min-pxcor]
    [ ignite ]
  set initial-trees count patches with [pcolor = green]
  set burned-trees 0
  reset-ticks
end

查看该错误,我发现一行上有

[ plants ]
,它看起来像一系列命令,但它没有相应的
ask
while
或类似的块。它上面的
ask patches with [(random-float 100) < density]
已经有一个命令列表,
[ set pcolor green ]
。所以我认为我们想要的是结合两个命令块:

to setup
  clear-all
  set-default-shape turtles "square" 
  ask patches with [(random-float 100) < density] [ 
    set pcolor green 
    plants 
  ]
  ask patches with [pxcor = min-pxcor]
    [ ignite ]
  set initial-trees count patches with [pcolor = green]
  set burned-trees 0
  reset-ticks
end

这里的错误是关于没有定义任何名为

plants
的内容,因为我们将其注释掉了。没关系,我们把它放回去吧:

to setup
  clear-all
  set-default-shape turtles "square"
  ask patches with [(random-float 100) < density] [ 
    set pcolor green 
    plants
  ]
  ask patches with [pxcor = min-pxcor]
    [ ignite ]
  set initial-trees count patches with [pcolor = green]
  set burned-trees 0
  reset-ticks
end

to plants
  [ ask neighbors4 with pcolor = black ; expected closing bracket error here
    set pcolor blue ]
    [ ask neighbors4 with pcolor = blue
      set pcolor grey ]

这里我们得到一个“预期结束括号”错误,但现在有了我们的知识,我们可以看到一些错误。括号括住条件子句、

with
和命令。另外,这个过程没有结束
end
,我们应该添加:

to plants
  ask neighbors4 with [pcolor = black] [
    set pcolor blue 
  ]
  ask neighbors4 with [pcolor = blue] [
    set pcolor grey 
  ]
end

之后,我们收到关于

ignite
中不存在
setup
的投诉,因此我们重新添加它。此时没有编译错误(
ignite
原样就很好):

globals [
  initial-trees
  burned-trees
]

breed [fires fire]

to setup
  clear-all
  set-default-shape turtles "square"
  ask patches with [(random-float 100) < density] [ 
    set pcolor green 
    plants
  ]
  ask patches with [pxcor = min-pxcor] [ 
    ignite 
  ]
  set initial-trees count patches with [pcolor = green]
  set burned-trees 0
  reset-ticks
end

to ignite
  sprout-fires 1
    [ set color yellow ]
  set pcolor black
  set burned-trees burned-trees + 1
end

to plants
  ask neighbors4 with [pcolor = black] [
    set pcolor blue 
  ]
  ask neighbors4 with [pcolor = blue] [
    set pcolor grey 
  ]
end

;to go
;  if not any? turtles
;    [ stop ]
;  ask fires
;    [ ask neighbors4 with [pcolor = green]
;     if random 100 < 50 [ignite]
;       [ ask neighbors4 with [pcolor = blue]
;           [ ask neighbors4 with [pcolor = grey]
;         if random 100 < 25 [ignite]
;  tick
;end
;

接下来的问题是取消注释

go
并解决出现的任何问题。

© www.soinside.com 2019 - 2024. All rights reserved.