在Android Studio中,我正在使用EditText
函数从caserCipher()
s获取文本和密钥。我敢肯定,这是正确的。但是,当将函数的返回值设置为viewText.text
时,它会打印出不期望的奇怪文本。
这是我的代码:
fun caserCipher(plainText: String, key: Int): CharArray {
val alphabet: String = "abcdefghijklmnopqrstuvwxyz"
val alphachar = alphabet.toCharArray()
val arrofChar = plainText.toCharArray()
var sizze:Int = plainText.length
var cipher = CharArray(sizze)
var ch :Char
var oriIndex:Int
var newIndex:Int
for (i in arrofChar.indices) {
if (Character.isLetter(arrofChar[i])) {
ch=arrofChar[i]
oriIndex= alphachar.indexOf(ch)
newIndex = ((oriIndex+key)%26)
ch= alphachar[newIndex]
cipher[i]=ch
} else {
cipher[i]=' '
}
}
return cipher
}
button.setOnClickListener {
val enteredText:String=editText.text.toString()
val key = editText2.text.toString()
val r = key.toInt()
val cipher=caserCipher(enteredText , r)
textView.text=cipher.toString()
}