没有那个特定的行,我的代码不起作用。为什么?

时间:2016-05-11 14:20:21

标签: ruby chomp

puts "Enter the first number"
num1 = Float(gets)

puts "Enter the second number"
num2 = Float(gets)

puts "Enter the operation"
op = gets
op = op.chomp # <--- THIS LINE!

case op 
when "+" then puts num1 + num2
when "-" then puts num1 - num2    
when "*" then puts num1 * num2    
when "/" then puts num1 / num2
end

1 个答案:

答案 0 :(得分:8)

进入&#34; +&#34;操作,你点击两个键, + 返回。两者都产生一个字符,产生"+\n"。 (\n是换行符)

chomp删除换行符。