2017-07-29 46 views
1

如何避免NOBODY運行時錯誤?以下是示例代碼。這是一個可用於零分割錯誤避免的代碼。因此,我知道它不能用於避免NOBODY錯誤。但我找不到任何其他方式。以下是運行時錯誤消息 - >「IFELSE-VALUE預期輸入爲TRUE/FALSE,但取而代之的是NOBODY。」我很欣賞你的建議。Netlogo如何避免NOBODY運行時錯誤?

set top ifelse-value (nobody) 
    [ 0 ][ top ] 
    set ts turtles with [speed = 0 and not right-end] 
    set top max-one-of turtles [who] 
    set topx [xcor] of top ; A NOBODY error appears in "of" of this code 
    set L count ts with [xcor > topx] 

回答

1

ifelse-value輸入必須是一個記者的回報或者truefalse(全部細節here。所以,如果你使用nobody作爲輸入,的NetLogo不評估輸入是否爲nobody與否,它只是讀取nobody - 換句話說,你的輸入是返回要麼truefalse

對於輸入,那麼,你需要改爲使用布爾變量(一個是要麼true或。 false),一個to-report返回truefalse,即可以的NetLogo計算表達式等考慮以下示例:

to go 

    let x true 
    set top ifelse-value (x) 
    ["x is true"] 
    ["x is NOT true"] 
    print (word "Example 1: " top) 

    set top ifelse-value (this-is-true) 
    ["Reporter returned true"] 
    ["Reporter did not return true"] 
    print (word "Example 2: " top) 

    set x nobody 
    set top ifelse-value (x = nobody) 
    ["x IS nobody"] 
    ["Is NOT nobody"] 
    print (word "Example 3: " top) 

    set x 0 
    set top ifelse-value (x = nobody) 
    ["x IS nobody"] 
    ["x Is NOT nobody"] 
    print (word "Example 4: " top) 

    set top ifelse-value (nobody = nobody) 
    ["nobody = nobody"] 
    ["nobody != nobody"] 
    print (word "Example 5: " top) 

end 

to-report this-is-true 
    report true 
end 
相關問題