我试图创建我的第一个应用程序之一,以帮助我理解Ruby的一些核心概念。对于大多数人来说,这是非常简单和自我解释的,我毫不怀疑。任何帮助是极大的赞赏。如果这看起来很愚蠢,我道歉,我刚刚开始并尽力解决这个问题。我想添加选项以查看列表,如果他们回答" no"到了#34;你想继续在你的清单中添加水果吗?"。我还希望应用程序,最后,最后的其他声明,让用户回到"然后告诉我你最喜欢的水果! (键入'完成'离开)"信息。我该怎么做呢?
# 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
答案 0 :(得分:1)
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"]
希望它有所帮助。