我的Ruby Cipher出了什么问题?

时间:2014-06-23 00:01:29

标签: ruby encryption cryptography

有人能告诉我我的代码出错了吗?我正在尝试创建一个一次性填充密码,它需要一个字符串对其进行加密,然后将其重新输出。但是当我把一根绳子放进去时,有人可以纠正我的工作,谢谢。

我的工作:

        h = {}
        v = 0

        ('A'..'Z').each do |c|
        v+=1
         h[c] = v
        end 

        puts "Provide Input:"
        input = gets
        input.downcase!


        if input.include?("a") 
            n = h["A"] + rand(26)
            puts h.index(n) 

        if input.include?("b")
            n = h["B"] + rand(26)
            puts h.index(n) 

        if input.include?("c")
            n = h["C"] + rand(26)
            puts h.index(n)  

        etc, etc... (All the way to the end of the alphabet)
           end
           end
       end

1 个答案:

答案 0 :(得分:0)

我没有看到任何'结尾'跟踪每个'if'块。如果是这种情况,那就意味着你的ifs是嵌套的,而你只是在满足前一个条件时检查下一个条件(如果没有'a'则不会做任何事情,如果有'a'则不会做太多'但没有'b'等)。此外,只要你的代码块几乎完全相同且只是少量变化,通常就会表明有一种更简单的方法来做你想做的事情。

相关问题