所以我需要在Smalltalk的愷撒密碼的代碼,並創建一個方法,並使用它,所以我可以做下面的測試就可以了創建愷撒密碼方法
|aString|
aString:=Caesar new encrypt: 'CAESAR'.
Transcript show: aString.
我已經類製造。但我需要制定它的方法。
我發現這個,但我怎麼能做出這個方法,所以我可以在操場上面的所有代碼。
| i c strCipherText strText iShiftValue iShift |
strText := 'the quick brown fox jumps over the lazy dog'.
iShiftValue := 3.
strCipherText := ''.
iShift := iShiftValue \\ 26.
i := 1.
[ i <= (strText size) ]
whileTrue: [
c := (strText at: i) asUppercase.
((c >= $A) & (c <= $Z))
ifTrue: [
((c asciiValue) + iShift > $Z asciiValue)
ifTrue: [
strCipherText := strCipherText, (((c asciiValue) + iShift - 26)
asCharacter asString).
]
ifFalse: [
strCipherText := strCipherText, (((c asciiValue) + iShift)
asCharacter asString).
].
]
ifFalse: [
strCipherText := strCipherText, ' '.
].
i := i + 1.
].
Transcript show: strCipherText.
Transcript cr.
因此,爲了使事情清楚,我需要使用愷撒密碼碼的方法,並在一開始就使用「ASTRING」的代碼,並與測試。我在上面有這個代碼,但是它已經有了文本,並且不能被放入方法中。
任何幫助將不勝感激。
當然,你可以把它放到一個方法。只需用方法參數替換'strText'即可。例如:該方法可能被稱爲'#myCaesorCodeOf:'並帶有一個名爲'aString'的參數。 –
@MaxLeske我是新來的使用pharo和小談話,所以我真的不知道該怎麼做。 –