Created
January 28, 2019 21:37
-
-
Save caioertai/9058c27a714d83f109730b5a12816ab4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def calculate(first_number, operator, second_number) | |
case operator | |
when '+' then first_number + second_number | |
when '-' then first_number - second_number | |
when '*' then first_number * second_number | |
when '/' then first_number / second_number.to_f | |
else "Invalid operator" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'calculator' | |
puts "Welcome to the calculator" | |
loop_input = 'y' | |
until loop_input == 'n' | |
puts "What's the first number you want to operate on?" | |
first_number = gets.chomp.to_i | |
puts "Which operator? (+|/|*|-)" | |
operator = gets.chomp | |
puts "What's the second number you want to operate on?" | |
second_number = gets.chomp.to_i | |
puts "The result is:" | |
result = calculate(first_number, operator, second_number) | |
puts result | |
puts "Do you want to calculate again? (y/n)" | |
loop_input = gets.chomp | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment