【发布时间】:2013-09-08 23:21:44
【问题描述】:
出于某种原因,这段代码总是将b (2.54) 乘以unit_number,无论我输入的是“cm”还是“in”:
puts "Welcome to my unit conversion calculator. This calculator can convert
between Centimeters and Inches. If you could like to convert Centimeters to Inches, write: in. If you would like to convert Inches to centimeters, write: cm."
unit = gets.to_s
puts " how many of your unit would you like to convert"
unit_number = gets.to_f
a = 0.39370079
b = 2.54
if unit == 'in'
puts "your conversion is: #{ (unit_number * a)}"
else unit == 'cm'
puts "your conversion is: #{ (unit_number * b)}"
end
【问题讨论】:
-
欢迎来到 Stack Overflow。请通过发布您已应用于该问题的一些properly formatted 代码、所有相关错误消息与它们显示的完全相同以及您正在测试的任何样本来改进您的问题。另外,请附上格式正确的预期输出样本,以便人们了解您想要达到的结果。
-
您好,抱歉,我也对这个问题感到困惑,您是否正在寻找这样的解决方案。 read input from console
-
推测
gets将等于cm或in。如果这是真的,那么gets.to_f将总是 等于0.0。我错过了什么吗? -
您无需输入
gets.to_s。gets是 get string 的缩写,这意味着无论如何你都会得到一个字符串。
标签: ruby unit-testing if-statement