将罗马数字转换为阿拉伯语的方法不会打印答案

时间:2016-03-24 12:09:34

标签: ruby

我正在创建一个将罗马数字转换为整数的方法。我写了这个:

def roman_to_integer(roman)
  roman = roman.upcase 

  num_I = roman.count("I").to_i 
  num_V = roman.count("V").to_i
  num_X = roman.count("X").to_i
  num_L = roman.count("L").to_i
  num_C = roman.count("C").to_i
  num_D = roman.count("D").to_i
  num_M = roman.count("M").to_i

  rom_num = { "I" => 1, 
              "V" => 5, 
              "X" => 10,
              "L" => 50,
              "C" => 100,
              "D" => 500,
              "M" => 1000 }

    while roman 
      if roman[0] == "M"
        integer = num_M * rom_num["M"] + num_D * rom_num["D"] + num_C * rom_num["C"] + num_L * rom_num["L"] + num_X * rom_num["X"] + num_V * rom_num["V"] + num_I * rom_num["I"]
      elsif roman[0] == "D"
        integer = num_D * rom_num["D"] + num_C * rom_num["C"] + num_L * rom_num["L"] + num_X * rom_num["X"] + num_V * rom_num["V"] + num_I * rom_num["I"]
      elsif roman[0] == "C"
        integer = num_C * rom_num["C"] + num_L * rom_num["L"] + num_X * rom_num["X"] + num_V * rom_num["V"] + num_I * rom_num["I"]
      elsif roman[0] == "L"
        integer = num_L * rom_num["L"] + num_X * rom_num["X"] + num_V * rom_num["V"] + num_I * rom_num["I"]
      elsif roman[0] == "X"
        integer = num_X * rom_num["X"] + num_V * rom_num["V"] + num_I * rom_num["I"]
      elsif roman[0] == "V"
        integer = num_V * rom_num["V"] + num_I * rom_num["I"]
      elsif roman[0] == "I"
        integer = num_I * rom_num["I"]
      end
    end
    return integer
end

puts "Tell me any number in Roman System and I will convert it to the Arabic Number!"
number = gets.chomp
puts "Here is your number:"
puts(roman_to_integer(number))

当涉及最后一行时,它应该打印出结果,我只是在命令行中获得空输入行。我不明白为什么它不起作用,需要一些帮助来找到代码错误的地方。

如果我使用

puts number.roman_to_integer

作为最后一行,我得到了

private method "roman_to_integer" called on "":String (NoMethodError)

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:2)

您的方法不会返回,因为它卡在while roman区块中 - 因为您不会更改roman变量,它将始终是真的,并且该块将继续运行一遍又一遍,永远不会返回一个值:

while roman
  #... runs forever
end

无论如何,您的方法似乎没有按照您的想法执行('IX'将返回1?)

答案 1 :(得分:0)

删除while循环。我不认为它会为代码添加任何内容。我还改进了格式并删除了多余的to_s方法。

def roman_to_integer(roman)

  roman = roman.upcase 

  # Counts the occurrences of each letter from the input

  num_I = roman.count("I") 
  num_V = roman.count("V")
  num_X = roman.count("X")
  num_L = roman.count("L")
  num_C = roman.count("C")
  num_D = roman.count("D")
  num_M = roman.count("M")

  # Assigns values to every Roman letter

  rom_num = { "I" => 1, 
              "V" => 5, 
              "X" => 10,
              "L" => 50,
              "C" => 100,
              "D" => 500,
              "M" => 1000 }

    # Multiplies the value of the letter by number of occurrences          


  if roman[0] == "M"
    integer  = num_M * rom_num["M"] + num_D * rom_num["D"] + num_C * rom_num["C"] + num_L * rom_num["L"] + num_X * rom_num["X"] + num_V * rom_num["V"] + num_I * rom_num["I"]
  elsif roman[0] == "D"
    integer = num_D * rom_num["D"] + num_C * rom_num["C"] + num_L * rom_num["L"] + num_X * rom_num["X"] + num_V * rom_num["V"] + num_I * rom_num["I"]

  elsif roman[0] == "C"
    integer = num_C * rom_num["C"] + num_L * rom_num["L"] + num_X * rom_num["X"] + num_V * rom_num["V"] + num_I * rom_num["I"]

  elsif roman[0] == "L"
    integer = num_L * rom_num["L"] + num_X * rom_num["X"] + num_V * rom_num["V"] + num_I * rom_num["I"]

  elsif roman[0] == "X"
    integer = num_X * rom_num["X"] + num_V * rom_num["V"] + num_I * rom_num["I"]

  elsif roman[0] == "V"
    integer = num_V * rom_num["V"] + num_I * rom_num["I"]

  elsif roman[0] == "I"
    integer = num_I * rom_num["I"]
  end

  return integer
end

puts "Tell me any number in Roman System and I will convert it to the Arabic Number!"

number = gets.chomp
puts "Here is your number:"
puts roman_to_integer(number)

另外,要使puts number.roman_to_integer工作,你必须将你的方法添加到String类中,顺便说一句,你不应该这样做。