M
M
Max2015-02-15 13:24:51
ruby
Max, 2015-02-15 13:24:51

Why doesn't a simple calculator come out in ruby?

Hello, tell me why my calculator does not recognize the signs + and -. This is my first Ruby program, so don't judge too harshly))
print "Enter the first number: "
a = gets.to_i
print "Enter the Second number: "
b = gets.to_i
print "Enter the operation symbol ('+' is an addition, and '-' is subtraction): "
opercacia = gets.to_s
plus = a+b
minus = ab
if opercacia == "+"
puts plus
elsif opercacia == "-"
puts minus
else puts "Something went wrong ( ( "
end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Vsk, 2015-02-15
@viktorvsk

What's wrong?
You need to look at what is in the variables, if something went wrong.
Well, in general, most likely, from the keyboard you enter not "+", but "+\n". Where \n is a newline. As a result, you do not meet the conditions. In this case, you can do something like:
But in general, this is a rather non-standard method for learning ruby ​​- start with console programs.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question