我正在尝试解析一个命令和一个int来在板上做一个"乌龟"移动.我有点不知所措,因为它没有抛出异常,我甚至无法弄清楚如何在没有它的情况下打开调试器.
我的代码:
"only should begin parsing on new line"
endsWithNewLine:= aTurtleProgram endsWith: String cr.
endsWithNewLine ifTrue:[
"splits commands based on new lines"
commands := aTurtleProgram splitOn: String cr.
commands do:[:com |
"should split into command and value for command"
i := com splitOn: ' '.
"command"
bo := ci at: 1.
val := ci at: 2.
"value for command"
valInt := val asInteger.
^ bo = 'down' "attempted switch"
ifTrue: [ turtle down: valInt ]
ifFalse: [
bo = 'left' …Run Code Online (Sandbox Code Playgroud) 一些样式指南和习语表明你不应该改变文字数组,就像在这种情况下:
MyClass>>incrementedNumbers
| numbers |
numbers := #( 1 2 3 4 5 6 7 8 ).
1 to: numbers size: do: [:index |
numbers at: index put: (numbers at: index) + 1].
^ numbers
Run Code Online (Sandbox Code Playgroud)
我为什么不这样做?
如果我逐个执行它们,以下Smalltalk代码将返回错误"context not not return".有人有解释吗?
f := [ :x :y | ^x + y].
sum:= f value: 3 value: 6.
Run Code Online (Sandbox Code Playgroud)
如果我一次执行它们,它会工作并按9预期返回.
我必须使用Smalltalk返回第n 个斐波那契数,我以前没有使用过这种语言。该程序将1返回到任何输入,我不知道为什么。我认为它甚至没有迭代for循环。有人可以帮我吗?谢谢。
'Which fibonacci number do you want? (n>2)' printNl.
n := stdin nextLine asInteger.
(n <= 2)
ifTrue: ['Type a larger number, F(1) and F(2) equals 1!' displayNl.]
ifFalse: [
result:= 1.
parent := 1.
gparent := 1.
2 to: n do: [ :i | [
result := (parent + gparent).
gparent := parent.
parent := result.
'come on, do something' displayNl.
]
].
result displayNl.
].
Run Code Online (Sandbox Code Playgroud) 给定矩形类中的两个整数变量'a'和'b',你如何绘制一个矩形?我是smalltalk的新手,我正在研究它.谢谢!