我是初学者,我一直在经历Learn to Program而且我已经在第8章了。
只想在while参数中解释'not'的用法。据我所知,虽然(不是goodAnswer)意味着它是真的,所以如果答案是'是'或'否'那么goodAnswer将是真的,而while循环应该继续。但事实并非如此,因为该计划已经结束。我可能会在这里遗漏一些关于“不”真正有用的东西。有人可以帮我理解这个吗?谢谢!
goodAnswer = false
while (not goodAnswer)
puts 'Do you like eating tacos?'
answer = gets.chomp.downcase
if (answer == 'yes' or answer == 'no')
goodAnswer = true
else
puts 'Please answer "yes" or "no".'
end
end
答案 0 :(得分:1)
while循环
我在第二行上稍微重写了代码,使其更加明确。它完全一样。
goodAnswer = false
while (not goodAnswer) == true
puts 'Do you like eating tacos?'
answer = gets.chomp.downcase
if (answer == 'yes' or answer == 'no')
goodAnswer = true
else
puts 'Please answer "yes" or "no".'
end
end
将(not goodAnswer)
视为单个实体。 goodAnswer = false
,(not goodAnswer) = true
。因此满足以下条件:
while (not goodAnswer) == true #condition satisfied, so the loop runs.
if 声明:
if (answer == 'yes' or answer == 'no')
goodAnswer = true
只要goodAnswer
等于 true ,(not goodAnswer)
自然等于 false 。所以现在的条件是:
while (not goodAnswer) == true
失败, while循环停止。
答案 1 :(得分:0)
如果答案是“是”'或者' no',然后goodAnswer是真的。然而,while循环的条件是"不是goodAnswer",这意味着与你声明的相反。它将是假的(不是goodAnswer == not true == false)。然后它将停止循环,因为它收到了有效的输入('是'或' no')。
基本上,"不是"意思是,与英语非常相似,你会说,"如果我还没有得到一个好的答案,那么......"。