我正在使用Ruby解释器来运行我创建的代码(一个简单的猜测数字1-100代码),但是每次在x次之后正确或错误地猜测数字时它会在打印后自动关闭出“你赢了!”或者“你输了!”。这是代码:
srand
random_number = rand 1..100
guesses = 10
while guesses > 0
puts "I'm thinking of a number between 1 and 100."
print "What number am I thinking of?"
guess = gets.chomp.to_i
guesses -= 1
break if guess == random_number
puts "Too high" if guess > random_number
puts "Too low" if guess < random_number
end
if guess == random_number
puts "You win!"
else
puts "You lose, sorry!"
end
如何防止它自行关闭,以便用户可以看到显示的消息?
答案 0 :(得分:0)
为什么不以:
结束puts "Hit enter to exit."
gets