如何使这个计算器工作?

时间:2016-06-15 00:15:40

标签: ruby

我只想处理一种方法,然后将其复制到其余方法。这就是为什么一种方法真的只做了。 (有点)

puts "Welcome to My Calculator! "  

print "Please place in the numbers. "  

first_number = gets.to_i  

print "Second number. "  

second_number = gets.to_i  


puts "What operation? "  

operation_selection = gets  

if(operation_selection == "add")  

  addition_function  

  puts"#{result}"

end  


def addition_function
 result = first_number + second_number

end

def subtraction_function
  result = first_number - second_number

end

def divison_function
   result = first_number / second_number

end

def multiplication_function
  result = first_number * second_number

end

1 个答案:

答案 0 :(得分:0)

这是你想要的吗?

puts "Welcome to My Calculator! "  

print "Please place in the numbers. "  

first_number = gets.to_i  

print "Second number. "  

second_number = gets.to_i  


puts "What operation? "  

operation_selection = gets.chomp.downcase.to_s  

def oper(operation_sel, first_number,second_number)
    case operation_sel
        when "add"
            puts first_number + second_number
        when "sub"
            puts first_number - second_number
        when "div"
            puts first_number / second_number
        when "mul"
            puts first_number * second_number
        end
end

oper(operation_selection,first_number,second_number)