簡體   English   中英

開始搶救重試錯誤

[英]Begin rescue retry error

如果此代碼執行else語句,我希望它從rescuebegin重試。 運行代碼會要求我輸入,而當我輸入代碼時,代碼將不起作用。

1-如何使此代碼與運行retry的else語句一起工作?

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

使用搶救塊用於處理異常,我真的認為這里不需要它。 循環將完成工作。

puts "What will you do?",
     "1- Get a closer look",
     "2- Go in the opposite direction",
     "Write your input and press enter:"

loop do
  choice = gets.to_i
  if choice == 1
    puts "You get a closer look and..."
    break
  elsif choice == 2
    puts "You go in the opposite direction, out of trouble"
    break
  else
    puts "Incorrect input, enter a number between the ones available:"
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM