我是第一次学习红宝石和计算机科学主题。我正在阅读 Chris Pine 所著的“学习编程”一书,并且对一个示例有疑问。
这是代码:
def ask(question) # new method with paramater question
while true # starts a loop
puts question # puts the question on the screen
reply = gets.chomp.downcase # gets the question and makes it lower case
if (reply == "yes" || reply == "no") # if reply was yes or no
if reply == "yes" # nested if statement if yes answer == true
answer = true # WHAT??
else # the else of the second if
answer = false # WHAT?? why do we care about answer??
end
break # Breaks the Loop
else # the else of the 1st if
puts ' Please answer "yes" or "no".'
end
end
answer # Why is answer here?
end
我的问题是为什么我们需要“答案”?我看不出它对循环有何影响。while 循环设置为 true 不回答。