【发布时间】:2013-10-03 19:35:58
【问题描述】:
我尝试执行的代码示例如下:
puts 'Please choose "A" or "B"?'
STDOUT.flush
@answer = gets.chomp.upcase
while @answer != "A" || "B"
puts 'Invalid answer, choose "A" or B"'
STDOUT.flush
@answer = gets.chomp.upcase
end
一旦输入 A 或 B,我怎样才能让它停止循环。如果我删除 While 循环,我可以让 @answer 成为 A 或 B,但是一旦我添加它,无论我输入什么,它都会不断循环。
【问题讨论】:
-
你需要
while @answer != "A" && @answer != "B"。运算符优先级在这里很重要。 -
如果对您有帮助,请接受stackoverflow.com/questions/19184727/…..
标签: ruby while-loop