So, basically, I am practicing if statements in Ruby and trying to make a simple calculator, but this puts out something so strange. If I enter an operator of "+" or any other operator it just does not do the calculation. I don't know why. I am genuinely clueless here. The code seems to be perfectly fine to me.
puts "Enter first number: "
num1 = gets.chomp().to_f
puts "Enter operator: "
op = gets.chomp().to_f
puts "Enter second number: "
num2 = gets.chomp().to_f
if op == "+"
puts (num1 + num2)
elsif op == "-"
puts (num1 - num2)
elsif op == "/"
puts (num1 / num2)
elsif op == "*"
puts (num1 * num2)
else
puts "Invalid operator"
end
Any operator I input I always get "Invalid operator" text on the screen instead of the calculation. If I remove the "invalid operator" else statements then nothing gets printed out.
Aucun commentaire:
Enregistrer un commentaire