0
基本上我必須在groovy上設計和實現這個,這是編碼和解碼一個特定的段落?如何爲字母替換密碼(groovy)創建編碼器和解碼器?
基本上我必須在groovy上設計和實現這個,這是編碼和解碼一個特定的段落?如何爲字母替換密碼(groovy)創建編碼器和解碼器?
你可以看看這個
http://groovy.codehaus.org/ExpandoMetaClass+-+Dynamic+Method+Names
其中顯示了編解碼器的典型使用情況。
基本上類似(從該鏈接)
class HTMLCodec {
static encode = { theTarget ->
HtmlUtils.htmlEscape(theTarget.toString())
}
static decode = { theTarget ->
HtmlUtils.htmlUnescape(theTarget.toString())
}
}
你不會使用HtmlUtils,但結構是一樣的。
編輯 - 這裏是一個關於如何做替換的例子。請注意,這可能可以更時髦,而且它並不處理標點符號,但它應該幫助
def plainText = 'hello'
def solutionChars = new char[plainText.size()]
for (def i = 0; i < plainText.size(); i++){
def currentChar = plainText.charAt(i)
if (Character.isUpperCase(currentChar))
solutionChars[i] = Character.toLowerCase(currentChar)
else
solutionChars[i] = Character.toUpperCase(currentChar)
}
def cipherText = new String(solutionChars)
println(solutionChars)
編輯 - 這裏是一個解決方案,是一個比較常規
def plainText = 'hello'
def cipherText = ""
plainText.each {c ->
if (Character.isUpperCase((Character)c))
cipherText += c.toLowerCase()
else
cipherText += c.toUpperCase()
}
println(cipherText)
哪一部分是你有問題嗎? – hvgotcodes 2011-01-06 20:04:39
基本上,我只是想要開始代碼的結構。 – ben 2011-01-06 20:17:28