问题:FACTORS=[["x",5],["z",3]],执行结果为 xz,但是 FACTORS=[[“c”,9],["x",5],["z",3]] 执行结果为 c,我预计的结果应该是 cxz,求问原因。 代码如下:
class Roman
def initialize(value)
@value = value
end
FACTORS=[["x",5],["z",3]] #返回xz
#FACTORS=[[“c”,9],["x",5],["z",3] #返回c
def to_s
value = @value
roman = ""
for code, factor in FACTORS
count, value = value.divmod(factor)
if !count.zero?
roman << code
end
end
roman
end
end
a = Roman.new(9).to_s
puts a