【问题标题】:Small currency converter [closed]小型货币转换器[关闭]
【发布时间】:2015-03-08 13:31:28
【问题描述】:

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

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"

【问题讨论】:

    标签: ruby converter currency


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-12
      • 2015-03-27
      • 1970-01-01
      • 2011-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多