小货币转换器

时间:2015-03-08 13:31:28

标签: ruby converter currency

我开始制作货币转换器。这就是代码的样子:

print"What currency would you like to convert from?(british,yen):"
currency_in = gets.to_s
print"What currency would you like to convert to?(british,yen):"
currency_out = gets.to_s
print"How much would you like to convert?:"
to_c = gets.to_f
if (currency_in == "british") && (currency_out == "yen")
  puts to_c * 181.69 + "Y"
elsif (currency_in == "yen") && (currency_out == "british")
  puts to_c * 0.01 
else
  puts"no such currency"
end

但它没有像我期望的那样起作用。当我输入british然后yen或其他方式时,程序会继续"no such currency"

1 个答案:

答案 0 :(得分:2)

gets将在String的末尾包含换行符("\n")。您可以使用chomp删除它:

currency_in = gets.chomp
print "What currency would you like to convert to?(british,yen):"
currency_out = gets.chomp

# ...