[英](Ruby) Counting in Loops
could you help me out, please? 你能帮我一下吗?
I want to write Ruby code in such a way that when I say the word "BYE!" 我想以这样的方式编写Ruby代码:当我说“再见!”一词时 3 times in a row, it terminates the program.
连续3次终止程序。
My code is below 我的代码如下
quotes = File.readlines('quotes.db')
puts = "What?"
print ">"
request = gets.chomp
while request != "BYE!"
puts quotes[rand(quotes.length)]
puts ">"
request = gets.chomp
end
Any I could amend the code to follow the rules I want? 我可以修改代码以遵循我想要的规则吗?
Check if this is what you want. 检查这是否是您想要的。 and tell me if any error occurs.
并告诉我是否发生任何错误。 this may be the rough code
这可能是粗糙的代码
quotes = File.readlines('quotes.db')
puts = "What?"
print ">"
counter = 0
request = gets.chomp
while counter < 3
counter += 1 if request.eqls?("BYE!")
puts quotes[rand(quotes.length)]
puts ">"
request = gets.chomp
end
I would do something like this: 我会做这样的事情:
quotes = File.readlines('quotes.db')
counter = 0
puts 'What?'
loop do
print '>'
request = gets.chomp
if request == 'BYE!'
counter += 1
break if counter >= 3
else
counter = 0
end
puts quotes.sample
end
puts 'If you type bye 3 times, this program will terminate'
bye_counter = 0
loop do
input = gets.chomp
if input == 'bye'
bye_counter += 1
else
bye_counter = 0
end
break if bye_counter == 3
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.