1
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
4

1 回答 1

8

输入“+”操作时,按两个键,+然后return。两者都产生一个字符,导致"+\n". (即\n换行符)

chomp删除换行符。

于 2016-05-11T14:26:18.577 回答