この単純なハイ/ロー推測ゲームで、ユーザーが 1 から 100 までの正の整数のみを入力できるようにする方法を見つけようとしています。1 から 100 までの整数以外は、すべての入力に対してそれを実行できないことをユーザーに通知するようにしたいと思います。これを行う簡単な方法はありますか?
私が今持っているもの:
count=0
play = true
while play == true
print "Give me a random number between 1 and 100: "
max= gets.to_i
num= rand(max)
puts "Now guess between 1 and " + max.to_s +
"."
print "What is your guess?: "
guess=gets.to_i
while guess != num && play != false
if guess > num
print "Too high! "
guess=gets.to_i
count+=1
elsif guess < num
print "Too low! "
guess=gets.to_i
count+=1
else
break
end
end
puts "Good Job! You figured it out in " + count.to_s + " attempts!"
print "Want to try again? y/n "
answer=gets.chomp!
if answer == 'n'
play = false
break
end
if
answer == 'y'
play = true
end
end
puts "Maybe next time..."