简单的if else语句单元转换

时间:2013-09-08 19:54:44

标签: ruby if-statement

我写了这段代码,但它没有用。我真的很感激帮助:

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

puts " how many of your unit would you like to convert"

a = 0.39370079

b = 2.54

unit_number = gets.to_f

if unit = cm

    (unit_number * a)

else unit = in

    (unit_number * b)
end

1 个答案:

答案 0 :(得分:1)

if语句没有做你想要的。要检查是否相等,请使用===是作业

if unit == cm

但你真的想要'cm',因为它是一个字符串。你需要将它包装在引号中,让运行时知道你的意思是一个字符串:

if unit == 'cm' 

最后,您需要向用户输出转换内容

puts unit_number * a

甚至更好

result = unit_number * a
puts "converting #{unit_number} to #{unit} is #{result}"