[英]Iterator in a while loop executes one more time than expected
I am trying to create a code that runs until a person says 'BYE'
three consecutive times. 我正在尝试创建一个代码,直到一个人连续三次说'BYE'
为止。
With the following code: 使用以下代码:
ask = gets.chomp
count = 0
while (count != 3)
if (ask == 'BYE')
puts 'HUH?! SPEAK UP, SONNY!'
count = count + 1
ask = gets.chomp
elsif (ask != ask.upcase)
puts 'HUH?! SPEAK UP, SONNY!'
count = 0
ask = gets.chomp
else
puts 'NO, NOT SINCE ' + rand(1930..1950).to_s + '!'
count = 0
ask = gets.chomp
end
end
puts 'Goodbye for now'
I actually need to type 'BYE'
four times. 我实际上需要输入'BYE'
四次。 Can someone point to how to fix it? 有人可以指出如何解决吗?
You don't actually have to type BYE four times. 您实际上不必四次键入BYE。 Just three is enough. 仅三个就足够了。 The fourth input can be anything. 第四输入可以是任何东西。
BYE # input 1
HUH?! SPEAK UP, SONNY!
BYE # input 2
HUH?! SPEAK UP, SONNY!
BYE # input 3
HUH?! SPEAK UP, SONNY!
nah, I give up # input 4
Goodbye for now
The problem with your code is that you request next input immediately after you have chosen a handler. 代码的问题是选择处理程序后立即请求下一个输入。 Even if, by your logic, you're not going to need that input. 即使按照您的逻辑,您将不需要该输入。 So you'll have to rework this part. 因此,您必须重新制作此部分。
Putting gets
only once at the beginning of the loop fixes it : 把gets
的循环开始时只有一次修复它:
count = 0
while count != 3
ask = gets.chomp
if ask == 'BYE'
puts 'HUH?! SPEAK UP, SONNY!'
count = count + 1
elsif ask != ask.upcase
puts 'HUH?! SPEAK UP, SONNY!'
count = 0
else
puts 'NO, NOT SINCE ' + rand(1930..1950).to_s + '!'
count = 0
end
end
puts 'Goodbye for now'
Execution : 执行:
$ ruby -w topc.rb
a
HUH?! SPEAK UP, SONNY!
A
NO, NOT SINCE 1931!
BYE
HUH?! SPEAK UP, SONNY!
BYE
HUH?! SPEAK UP, SONNY!
BYEE
NO, NOT SINCE 1939!
BYE
HUH?! SPEAK UP, SONNY!
BYE
HUH?! SPEAK UP, SONNY!
BYE
HUH?! SPEAK UP, SONNY!
Goodbye for now
Your while (count != 3)
is so Javaish (no need for parentheses in Ruby) that I can't resist to write a more rubyesque solution. 您的while (count != 3)
是如此的Java风格(在Ruby中不需要括号),我无法抗拒编写一个更红宝石的解决方案。 Not shorter, but more DRY, with more code that you are likely to see in Ruby programs, and certainly not exemplary, there are always many ways to do the same thing in Ruby. 它不是更短,而是更干,具有更多的代码,您可能会在Ruby程序中看到更多的代码,并且当然不是示例性的,在Ruby中总是有很多方法可以执行相同的操作。
class Strange
def initialize(wanted)
@answer = true
@count = 0
@wanted = wanted # desired number of correct consecutive answers
end
# Increment @count if true, else reset to zero.
def answer(boolean)
@answer = boolean
if boolean
then # then is optional, but I like it
@count = @count + 1
else
@count = 0
end
end
# Write a message.
def message(number)
puts case number
when 1 then 'HUH?! SPEAK UP, SONNY!'
when 2 then "NO, NOT SINCE #{rand(1930..1950)} !"
else 'WHAT ?'
end
end
def prompt
print @answer ? 'Talk please > ' : 'Wrong answer, retry > '
@ask = gets.chomp
end
# Recursively loop until the number of correct consecutive answers
# corresponds to the desired number.
def run
prompt
case
when @ask == 'BYE'
message 1
answer(true)
when @ask != @ask.upcase
message 1
answer(false)
else
message 2
answer(false)
end
run unless @count == @wanted # recursive loop
end
end # class Strange
Strange.new(3).run
puts 'Goodbye for now'
Execution : 执行:
$ ruby -w t.rb
Talk please > xyz
HUH?! SPEAK UP, SONNY!
Wrong answer, retry > XYZ
NO, NOT SINCE 1935 !
Wrong answer, retry > BYE
HUH?! SPEAK UP, SONNY!
Talk please > what ?
HUH?! SPEAK UP, SONNY!
Wrong answer, retry > BYE
HUH?! SPEAK UP, SONNY!
Talk please > BYE
HUH?! SPEAK UP, SONNY!
Talk please > BYE
HUH?! SPEAK UP, SONNY!
Goodbye for now
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.