试图在Ruby中为计算器创建一个循环

时间:2014-04-14 23:35:27

标签: ruby loops while-loop

我希望我不会太讨厌。我在2周前开始使用红宝石。 我试图将我的计算器放在循环中,以便在计算结束后重新启动。喜欢"你想再试一次吗?"

def add(a, b)
  puts "ADDING #{a} + #{b}"
  puts "The result is #{a + b}"
end


def arg1()
puts "You chose option 1"
print "please enter first entry "
first_number = gets.to_i
print "Please  enter second entry "
second_number = gets.to_i
add(first_number,second_number)

end


def selection()
puts "please enter your option : "
puts "For Adding : 1 "
puts "For Subtacting : 2 "
print "> "
end


selection
options_of_choice = gets.to_i

  if options_of_choice == 1
    arg1()

  elsif options_of_choice == 2
    arg2()

  else
    puts " Restarting"

  end

1 个答案:

答案 0 :(得分:2)

calculator_on = true
while calculator_on
  selection
  options_of_choice = gets.to_i
  if options_of_choice == 1
    arg1()
  elsif options_of_choice == 2
    arg2()
  else
    puts " Restarting"
  end
 puts "do you want to try again?"
 calculator_on = gets.chomp.downcase == 'y'
end