首页 > 解决方案 > 使用用户输入中断 while 循环

问题描述

我如何通过用户输入打破循环 除了是之外的示例输入只会让我打破循环 在此处输入图像描述

标签: rubyloopswhile-loop

解决方案


def main
  loop do 
    # ...

    puts "Repeat for another input?"
    ans = gets.chomp
    if ans != "yes"
      break # will break the loop
    end
  end
end

main

推荐阅读