尝试在Ruby中为计算器创建循环
我希望我不是太烦人。我两周前开始学习ruby。 我试图把我的计算器放在一个循环中,这样它在计算结束后重新启动。比如“你想再试一次吗?”尝试在Ruby中为计算器创建循环,ruby,loops,while-loop,Ruby,Loops,While Loop,我希望我不是太烦人。我两周前开始学习ruby。 我试图把我的计算器放在一个循环中,这样它在计算结束后重新启动。比如“你想再试一次吗?” 您不应该命名方法arg。 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 = get
您不应该命名方法
arg
。
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
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