好的,我正在尝试为祖母写一个ruby模拟.我不能让循环按照我想要的方式工作.我想让奶奶回应
“OH,THAT REMINDS ME OF BACK IN (random year) …”
当你全部回答她时,我也希望她回答
“WHAT’D YOU SAY????”
当你不使用全部大写.我可以让每个人分开工作,但我似乎无法用她的疯狂反应做出一个连续的奶奶循环.这是代码:
puts 'HELLO SONNY! WHAT\'S NEW IN THE WHO\'S IT WHAT\'S IT?' response = gets.chomp while response == response.upcase puts 'OH,THAT REMINDS ME OF BACK IN ' + (rand(50) + 1905).to_s + '...' response = gets.chomp end while response != response.upcase puts 'WHAT\'D YOU SAY????' response = gets.chomp end
有任何想法吗?
解决方法
问题是,一旦你退出第一个while循环,你就永远不会回到它.尝试这样的事情:
while true response = gets.strip if response == response.upcase puts msg1 else puts msg2 end end
这将永远运行,直到你决定用Ctrl-C杀死虚拟奶奶.