def multiply(num_1,num_2)
  return num_1.to_f*num_2.to_f
end
# return을 안붙여도 작동 됨

num_1=gets.chomp
num_2=gets.chomp

puts "#{multiply(num_1,num_2)}"

# 예제
def multiply(num_1,num_2)
  return num_1.to_f * num_2.to_f
end
 
def divide(num_1,num_2)
  return num_1.to_f / num_2.to_f
end
 
def subtract(num_1,num_2)
  return num_1.to_f - num_2.to_f
end
 
def remainder(num_1,num_2)
  return num_1.to_f % num_2.to_f
end
 
def result(num)
  puts "answer is #{num}"
end
 
puts "simple calculator"
20.times {print "-"}
puts
puts "first number"
num_a = gets.chomp()
puts "second number"
num_b = gets.chomp()
puts "what do you want to do? 1) multiply 2) divide 3) subtract 4) find remainder"
prompt = gets.chomp()
 
if prompt == "1"
  puts "you have chosen to find multiply"
  result(multiply(num_a, num_b))
elsif prompt == "2"
  puts "you have chosen to find divide"
  result(divide(num_a, num_b))
elsif prompt == "3"
  puts "you have chosen to find subtract"
  result(subtract(num_a, num_b))
elsif prompt == "4"
  puts "you have chosen to find remainder"
  result(remainder(num_a, num_b))
else
  puts "you have made an invalid choice"
end

'ruby on rails' 카테고리의 다른 글

ruby project1  (0) 2020.11.08
ruby hashes  (0) 2020.11.08
ruby arrays and iterators  (0) 2020.11.08
ruby 기본 입출력  (0) 2020.11.07
rails 공부시작  (0) 2020.11.07

+ Recent posts