weeks1to2/Example/calculator.rb
2017-08-14 11:34:33 +10:00

59 lines
1.4 KiB
Ruby

# This method ask the user what type of calculation they would like to perform
# It returns the operation or an error for erroneous entry
def request_calculation_type
puts "Type 1 to add, 2 to subtract, 3 to multiply, or 4 to divide two numbers: "
operation_selection = gets.to_i
if operation_selection == 1
"add"
elsif operation_selection == 2
"subtract"
elsif operation_selection == 3
"multiply"
elsif operation_selection == 4
"divide"
else
"error"
end
end
# This method performs the requested calculation
# It returns the result of the calculation
def calculate_answer(operator, a, b)
if operator == "add"
a + b
elsif operator == "subtract"
a - b
elsif operator == "multiply"
a * b
elsif operator == "divide"
a / b
end
end
run_calculator = 1
while run_calculator == 1
current_calculation = request_calculation_type()
if current_calculation == "error"
puts "I do not understand this type of calculation... Can we try again?"
else
puts "What is the first number you would you like to #{current_calculation}: "
first_number = gets.to_i
puts "What is the second number you would like to #{current_calculation}: "
second_number = gets.to_i
answer = calculate_answer(current_calculation, first_number, second_number)
puts "The answer is #{answer}"
puts "Type 1 to run another calcution or 2 to end: "
run_calculator = gets.to_i
end
end