如果此代码执行else
语句,我希望它从rescue
或begin
重试。
运行代码要求我输入,当我输入代码时,代码不起作用。
1-如何使此代码与运行retry
的
2-为什么删除rescue
会创建retry Syntax Error
?
# Creates input method
def input
x = gets.to_i
end
#Begins the first choice
begin
puts "What will you do?
1- Get a closer look
2- Go in the opposite direction
Write your input an press enter:"
rescue
#Calls input method
choice = input
if choice == 1
puts "You get a closer look and..."
elsif choice == 2
puts "You go in the opposite direction, out of trouble"
else
puts "Incorrect input, enter a number between the one's avaliables:"
end
#Retries if the choice is error
retry if choice != 1||2
end
答案 0 :(得分:1)
使用救援块用于处理异常,我真的不认为这里需要它。循环将完成这项工作。
next