whileTrue 在块闭包 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: 迭代中,xy 都被初始化为 1,然后在 whileTrue: 中,被初始化为 {{1} } 和 0.8。因此,1.2 的第一个赋值为 mapCoord,第二个赋值为 #[1 1 1 1 1],因为 1#[1 1 1 1 1] at: 1。因此,1 变为 t2false 退出。

更清楚

whileTrue:
,

从语法上讲,它没有理由不工作,因为您可以运行代码,所以您的问题的答案是:是的,您可以! 如果您认为 while 循环没有按照您的预期执行,我建议您不要“执行”,而是尝试“调试”命令。这将允许您逐条执行代码语句。您可以一路检查所有变量,也许可以找出问题所在。

关于您的代码的一些说明:

  • 您单独使用 x/y,但实际上您在谈论 Point,它具有 x 和 y 属性。它还支持号码协议的大部分内容,这意味着您也可以将消息 rounded 发送到 Point。您可以通过 Point 创建 @ 对象,例如:point := 4 @ 5。点还允许像 point * 4point + something 这样的算术。
  • rounded 返回 Integer,因此无需通过 asInteger 显式转换它。
  • 您有一个方法 asRAD 但已经有一个系统方法 degreesToRadians
  • 你不应该给变量编号。反编译器这样做是因为它不知道更好,但如果你不知道如何命名你的变量,至少用它们的类型来命名它们。 t4 可能是 aComposite