1

我写了这段代码,但它不工作。我真的很感激帮助:

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
4

1 回答 1

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}"
于 2013-09-08T20:02:55.520 回答