无法使用Kotlin在Android Studio中打印

时间:2019-11-30 10:20:16

标签: kotlin

在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()

}

0 个答案:

没有答案