gpt4 book ai didi

kotlin - Kotlin-如何实现围栏密码?

转载 作者:行者123 更新时间:2023-12-02 13:26:55 25 4
gpt4 key购买 nike

我正在研究Rail Fence Cipher加密。

fun main(){
println("Enter the message:")
var message:String = readLine()!!.toUpperCase()
println("Enter the key:")
var key:Int = readLine()!!.toInt()
var skip:Int = 2*(key-1)
var j=0
var encryptedMsg:String=""
var key1 = key

while(key1>0){
for(i in (key-key1)..message.length step skip){
if(i<=message.length-1)
encryptedMsg+=message[i]
}
key1-=1
skip = 2*(key1-1)
}
println("Encrypted Message: $encryptedMsg")
}
I am having trouble with the last row.
在这里,我得到一个错误: Step must be positive, was: 0。我也知道步骤对最后一行不起作用。那么还有其他选择吗?

最佳答案

fun main(){
println("Enter the message:")
var message:String = readLine()!!.toUpperCase()
println("Enter the key:")
var key:Int = readLine()!!.toInt()
var skip:Int = 2*(key-1)
var j=0
var encryptedMsg:String=""
var key1 = key

while(key1>0){
for(i in (key-key1)..message.length step skip){
if(i<=message.length-1)
encryptedMsg+=message[i]
}
key1-=1
skip = 2*(key1-1)
if(skip==0) skip=2*(key-1) //This solves the problem
}
println("Encrypted Message: $encryptedMsg")
}
不过,如果有人在没有两个循环的情况下有更好的建议,我想知道。谢谢。

关于kotlin - Kotlin-如何实现围栏密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63414402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com