如何将用户引导回嵌套 if 语句的开头?

How do you direct a user back to the beginning of a nested if statement?

我正在尝试创建我的第一个应用程序来帮助我理解 Ruby 的一些核心概念。对于你们大多数人来说,这是非常简单和不言自明的,我毫不怀疑。任何帮助是极大的赞赏。如果这看起来很愚蠢,我深表歉意,我才刚刚开始并尽力解决这个问题。如果他们回答 "no" 到 "Do you want to keep adding fruits to your list?",我想添加查看列表的选项。我还希望应用程序在最后的 else 语句中将用户带回 "Then tell me another of you favorite fruits! (Type 'done' to get out)" 消息。我该怎么做呢?

# This app was created by: Daniel Horowitz

fruits = []

puts "Please tell me what one of your favorite fruits are... Do tell."
input = gets.chomp
fruits << input

puts "Yummy, that sounds delicious. You must tell me another!"
input = gets.chomp
fruits << input

puts "Do you want to keep adding fruits to your list?"
answer = gets.chomp.downcase

if answer == "yes"
  puts "Then tell me another of your favorites fruits! (Type 'done' to get out)"
  input = gets.chomp
  while input != "done"
    fruits << input
    puts "Would you like to see a list of you most favorite fruits?"
    input2 = gets.chomp.downcase
    if input2 == "yes"
      puts "These are your most dilectably delicious favorite fruits: #{fruits}"
    else

    end
  end
end
fruits = []

loop do # endless loop; see break
  puts "Type your fave fruit or “done” to exit:"
  input = gets.chomp

  break if input == 'done' # break a loop if “done” was entered
  fruits << input
end

puts "Would you like to see a list of you most favorite fruits?"
if gets.chomp.downcase == "yes"
  puts "These are your most dilectably delicious favorite fruits: #{fruits}"
end

运行:

# Type your fave fruit or “done” to exit:
Apple
# Type your fave fruit or “done” to exit:
Orange
# Type your fave fruit or “done” to exit:
done
# Would you like to see a list of you most favorite fruits?
yes
# These are your most dilectably delicious favorite fruits: ["Apple", "Orange"]

希望对您有所帮助。