在 Ruby 上制作计算器时出错?

Error while making calculator on Ruby?

尝试在 Ruby 中制作计算器时,使用 TextWrangler 的 #!函数(在终端中编译)我遇到了错误。当我添加 2+2 时,答案 returns 是 2.0。我尝试了其他功能,例如计算某个数字的特定百分比,无论我尝试做什么,答案始终是 0.0。我检查了语法,使用 #!函数,没有错误。我知道我犯了不必要的错误,但对我来说这样读起来更容易。

loop do
print 
equation = gets.chomp

if equation.include?"^"
    exponent_e = equation.split("^")
    result_e = equation[0].to_f ** equation[1].to_f
    puts "#{equation} = #{result_e}"
elsif equation.include?"%"
    percent_e = equation.split("%")
    number = equation[0].to_f / 100
    result_p = number * equation[1].to_f
    puts "#{equation} = #{result_p}"
elsif equation.include?"/"
    equation.split("/")
    result_d = equation[0].to_f / equation[1].to_f
    puts "#{equation} = #{result_d}"
elsif equation.include?"*"
    equation.split("*")
    result_m = equation[0].to_f * equation[1].to_f
    puts "#{equation} = #{result_m}"
elsif equation.include?"+"
    equation.split("+")
    result_a = equation[0].to_f + equation[1].to_f
    puts "#{equation} = #{result_a}"
elsif equation.include?"-"
    equation.split("-")
    result_s = eqaution[0].to_f - equation[1].to_f
    puts "#{equation} = #{result_s}"
end
end

您没有将拆分存储在变量中。你需要做这样的事情:

elsif equation.include?"+"
    res = equation.split("+")
    result_a = res[0].to_f + res[1].to_f