因此,当用户键入done
并从那里跳转到另一个名为" survey"的方法时,我试图打破一个循环。提出一些遗留问题。
这就是我现在所拥有的:
def survey
puts "For how many years have you had these allergies?"
name = gets.chomp.to_i
puts "When was your last allergy reaction?"
reaction = gets.chomp.to_i
end
def allergies
puts "Please enter all your allergies. When you are finished, type 'done'."
allergy = gets.chomp.downcase!
while allergy != "done"
# Format the loop so that when the user types 'done'
# The loop breaks and we go to the 'survey' method
allergy = gets.chomp.downcase
survey
if allergy == "nuts"
puts "Beware! The food item contains peanuts!"
break
end
end
end
allergies
目前它只是让我输入一个过敏症,然后跳过调查方法。我该如何改变这一点,以便我可以继续输入过敏症,直到我输入"坚果"或者"完成"?
答案 0 :(得分:1)
尝试if-else
声明
def allergies
puts "Please enter all your allergies. When you are finished, type 'done'."
allergy = gets.chomp.downcase!
while allergy != "done"
if allergy == "nuts"
puts "Beware! The food item contains peanuts!"
break
else
survey
end
allergy = gets.chomp.downcase
end
end
答案 1 :(得分:1)
def survey
puts "For how many years have you had these allergies?"
name = gets.chomp.to_i
puts "When was your last allergy reaction?"
reaction = gets.chomp.to_i
end
def allergies
puts "Please enter all your allergies. When you are finished, type 'done'."
allergy = gets.chomp.downcase!
while allergy != "done"
# Format the loop so that when the user types 'done'
# The loop breaks and we go to the 'survey' method
allergy = gets.chomp.downcase
if allergy == "nuts"
puts "Beware! The food item contains peanuts!"
break
elsif allergy == "done"
survey
end
end
end
allergies