Smalltalk 当你真的在一个街区闭门造车的闲聊?
你能用whileTrue吗:在一个封闭的街区里?我不能让它工作,所以我怀疑不能。或者我做错了什么Smalltalk 当你真的在一个街区闭门造车的闲聊?,smalltalk,visualworks,Smalltalk,Visualworks,你能用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
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
,然后在过程中:
被初始化为0.8
和1.2
之间的值。因此,mapCoord
的第一个赋值计算为#[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"
x:=posx。“x=1”
y:=波西。“y=1”
gsin:=(0.2*(rot_i sin))。
全球气候观测系统:=(0.2*(rot_i cos))。
t2:=真。
[t2=真]
尽管如此:[
x:=(x+gcos)。“0.8从语法上讲,没有理由认为它不应该工作,因为您可以运行代码,您的问题的答案是:是的,您可以!
如果您认为while循环没有达到预期效果,我建议您不要“执行”,而是尝试使用“debug it”命令。这将允许您一步一步地检查代码语句。您可以一路检查所有变量,并可能找出错误所在
关于代码的一些备注:
- 您单独使用x/y,但实际上您指的是具有x和y属性的
点。它还支持大部分数字协议,这意味着您也可以将消息四舍五入发送到点。您可以通过@
创建点对象,例如:点:=4@5
>.点还允许使用诸如point*4
或point+something
之类的算术
rounded
返回一个整数
,因此无需通过asInteger
显式转换它
- 您有一个方法
asRAD
,但已经有一个系统方法degreestoraris
- 你不应该给变量编号。反编译器这样做是因为它不知道如何更好地命名变量,但如果你不知道如何命名变量,至少要按照变量的类型命名。
t4
可以是a组合
我没有带VW去检查,但是在法罗,映射
数组的定义不起作用,你必须用
预先设置每个字节数组
。例如(#[1 1 1 1 1]#[1 0 0 0 1]…
。还要注意,n
在计算n:=(n+1)时没有定义
。它是ivar吗?还是您在一个自动定义变量的工作区中?是的,一个块可以在一个块中。您的观察结果是“它只是在”do:“块中进行下一次迭代“建议mapCoord=1
的频率比您预期的要高。如果行为与您预期的不同,请添加一些调试代码。可以打印到转录本,也可以添加一个暂停/断点。数组似乎可以工作,在工作区中运行。但我更改了它。然后我将条件(mapCoord=1)更改为(mapCoord<1).然后我得到了预期的行为。我想我没有意识到是这种情况导致了我的问题。谢谢。我稍后会深入研究你的代码(我相信Leandro会给你一个很好的答案)。我只想对编码样式进行注释。您的代码很难阅读,因为您不使用任何注释,也不使用变量,如t1、t2、t3、h等
。这些变量无助于阅读您的代码。