【发布时间】:2020-01-22 21:37:01
【问题描述】:
我想通过键值递增 ASCII 来加密字符串。但是使用这段代码我遇到了问题。
def caesar_cipher(str, key)
new_sentence = []
str.split("").each do |letter|
ascii = letter.ord
puts ascii
ascii += key if ascii >= 65 && ascii <= 90
if ascii > 90
return_start = ascii - 90
ascii = 64 + return_start
end
ascii += key if ascii >= 97 && ascii <= 122
if ascii > 122
return_start = ascii - 122
ascii = 96 + return_start
end
puts ascii
new_sentence << ascii.chr
end
puts new_sentence
end
caesar_cipher("Wh", 5)
我放了一些puts 来看看会发生什么,当我puts ascii 看到它并没有给我返回好数字时。对于'W',一切都很好。他从87 开始,然后转到66。但我不明白为什么'h'有问题。他从104 开始,然后转到78。为什么他不去109?
【问题讨论】:
标签: ruby conditional-statements ascii chr ord