代码如下

puts "input num1"
num1 = gets

puts "input type +-*/"

fh = gets

puts "input num2"

num2 = gets

case fh.chomp
when "+"
  # to_i:把一个字符串转换成数字
  #
  num = num1.to_i + num2.to_i
when "-"
  num = num1.to_i - num2.to_i
when "*"
  num = num1.to_i * num2.to_i
when "/"
  num = num1.to_i / num2.to_i

end

# chomp:去除字符串默认的分隔符,通常是\n
puts "Result:#{num1.chomp}#{fh.chomp}#{num2.chomp}=#{num}"

 

这里主要是掌握字符串的如下2个方法

to_i :转换一个字符串为数字

 

chomp:取出字符串默认的分隔符,一般都是\n

 

posted on 2019-06-26 15:30  bainianminguo  阅读(483)  评论(0编辑  收藏  举报