[英]whileTrue in a block closure Smalltalk?
你能在塊閉包中使用 whileTrue: 嗎? 我無法讓它工作,所以我懷疑不是。 或者我做錯了什么。
map := #([1 1 1 1 1]
[1 0 0 0 1]
[1 0 1 0 1]
[1 0 0 0 1]
[1 1 1 1 1]).
posx := 1.
posy := 1.
t4 := CompositePart new.
rot := (Core.Double.Pi / 4).
perspective := Core.OrderedCollection new.
1 to: 60 do: [:i | perspective add: (rot + (i - 30) asRAD).
rot_i := perspective at: i.
x := posx.
y := posy.
gsin := (0.2 * (rot_i sin)).
gcos := (0.2 * (rot_i cos)).
t2 := true.
[t2 = true]
whileTrue: [
x := (x + gcos).
y := (y + gsin).
n := (n + 1).
mapCoord := map at: x rounded asInteger.
mapCoord := (mapCoord at: y rounded asInteger).
(mapCoord = 1)
ifTrue: [
h := 1/(0.2 * n).
t2 := false]].
t3 := (i @ h extent: 2 @ h ) asFiller.
t4 add: t3].
如果 mapCoord 不等於 1,我想讓 whileTrue 塊中的所有內容都重復。但事實並非如此,它只是突然進入“do:”塊中的下一次迭代。 我懷疑我做錯了什么,但我想不通。
這就是我所看到的。
在每次do:
迭代中, x
和y
都被初始化為1
,然后在whileTrue:
中初始化為0.8
和1.2
之間的值。 因此, mapCoord
的第一個賦值計算為#[1 1 1 1 1]
,第二個賦值為1
因為#[1 1 1 1 1] at: 1
是1
。 因此, t2
變為false
並且whileTrue:
退出。
更清楚
x := posx. "x = 1"
y := posy. "y = 1"
gsin := (0.2 * (rot_i sin)).
gcos := (0.2 * (rot_i cos)).
t2 := true.
[t2 = true]
whileTrue: [
x := (x + gcos). "0.8 <= x <= 1.2"
y := (y + gsin). "0.8 <= y <= 1.2"
n := (n + 1).
mapCoord := map at: x rounded asInteger. "mapCoord = #[1 1 1 1 1]
mapCoord := (mapCoord at: y rounded asInteger). "mapCoord = 1"
(mapCoord = 1)
ifTrue: [
h := 1/(0.2 * n).
t2 := false]]. "t2 = false"
從語法上講,它沒有理由不工作,因為你可以運行代碼,所以你的問題的答案是:是的,你可以,如果你認為 while 循環沒有達到你的預期,我建議你不要“做”。 而是嘗試“調試它”命令。 這將允許您逐個語句地逐步執行您的代碼語句。 您可以一路檢查所有變量,並可能找出問題所在。
關於您的代碼的一些說明:
Point
,它具有 ax 和 y 屬性。 它還支持大部分數字協議,這意味着您也可以將消息rounded
到Point
。 您可以通過@
創建Point
對象,例如: point:= 4 @ 5
。 點還允許像point * 4
或point + something
這樣的算術。rounded
返回Integer
,因此無需通過asInteger
顯式轉換它。asRAD
但已經有一個系統方法degreesToRadians
t4
可以是aComposite
[:i| | eye x y gsin gcos coordinate notLessThanOne h |
" So I can see it too. Yes--A while loop inside a block is fine "
eye := perspective add: (rot + (i - 30) asRAD).
X := posx. " x = 1 "
y := posy. " y = 1 "
gsin := (0.2 * (eye sin)).
gcos := (0.2 * (eye cos)).
coordinate := notLessThanOne.
[ coordinate < 1 " op changed to < "
] whileFalse:
[x := (x + gcos). " 0.8 <= x <= 1.2 "
y := (y + gsin). " 0.8 <= y <= 1.2 "
n := (n + 1). " grows forever? "
coordinate := map at: x rounded." is #[1 1 1 1 1] "
coordinate := coordinate at: y rounded." is 1 "
].
h := 1/(0.2 * n).
parts add: (1@h extent: 2@h) asFiller
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.