输入字符串时循环不断开

时间:2016-01-11 01:54:22

标签: ruby loops

所以我只是制作了一个随机代码生成器并添加了一个循环,这样你就可以再次运行而不再运行它。但显然当我在最后输入“是”时会再次输入,但是当我输入“否”时仍会再次输入。

这是我的代码:

loop do
puts "Hello. I am a random code generator. PLease type in how many digits you want"
b = gets.chomp.to_i
a = rand *10**b
puts "Your random code is #{a.to_i}"
puts "Would you like to go again?"
x = gets
  if x == 'no'
 break
end
end

1 个答案:

答案 0 :(得分:1)

loop do
  puts "Hello. I am a random code generator. PLease type in how many digits you want"
b = gets.chomp.to_i
a = rand *10**b
  puts "Your random code is #{a.to_i}"
  puts "Would you like to go again?"
x = gets.chomp
if x == 'no'
    break
  end
end