Ruby 中的简单高低游戏

发布于 2024-10-15 06:18:38 字数 1347 浏览 2 评论 0原文

我正在尝试编写一个简单的高低程序,其输出如下:

$ ./highlow.rb 
Please tell me the max value of the random number: 100
Ok. The random number is generated between 1 and 100.  
Make your guess: 50
That's too low.  Guess again: 75
That's too high.  Guess again: 63
That's too low.  Guess again: 68
Correct!  You guessed the answer in 4 tries!
Would you like to play again? no
OK. Goodbye.
$

这是我的代码,但我在某个地方出错了。它有时似乎有效:


count=0
play = true
while play = true 

print "Please tell me the max value of the random number: "
    max= gets.to_i
    num= rand(max)
puts "Ok. The random number is generated between 1 and " + max.to_s + 
"."
print "Make your guess: "
    guess=gets.to_i

    while guess != num && play != false 
        if guess > num
            print "That's too high. Guess again: "
            guess=gets.to_i
            count+=1
        end

        if guess < num
            print "That's too low. Guess again: "
            guess=gets.to_i
            count+=1
        else
            break
        end
end

提出“正确!您在“+ count.to_s +”尝试中猜到了答案!” print "你想再玩一次吗?"

answer=gets.chomp! 如果答案=='n' 播放=假 休息 结束

if 
    answer == 'y'
    play = true
end 

结束 放“好吧。再见”

有什么建议吗?我哪里出错了?

I'm trying to write a simple high-low program that will output like this:

$ ./highlow.rb 
Please tell me the max value of the random number: 100
Ok. The random number is generated between 1 and 100.  
Make your guess: 50
That's too low.  Guess again: 75
That's too high.  Guess again: 63
That's too low.  Guess again: 68
Correct!  You guessed the answer in 4 tries!
Would you like to play again? no
OK. Goodbye.
$

This is the code I have, but I'm going wrong somewhere. It appears to work sometimes:


count=0
play = true
while play = true 

print "Please tell me the max value of the random number: "
    max= gets.to_i
    num= rand(max)
puts "Ok. The random number is generated between 1 and " + max.to_s + 
"."
print "Make your guess: "
    guess=gets.to_i

    while guess != num && play != false 
        if guess > num
            print "That's too high. Guess again: "
            guess=gets.to_i
            count+=1
        end

        if guess < num
            print "That's too low. Guess again: "
            guess=gets.to_i
            count+=1
        else
            break
        end
end

puts "Correct! You guessed the answer in " + count.to_s + " tries!"
print "Would you like to play again? "

answer=gets.chomp!
if answer == 'n'
play = false
break
end

if 
    answer == 'y'
    play = true
end 

end
puts "OK. Goodbye"

Any suggestions? Where am I going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不寐倦长更 2024-10-22 06:18:38

我认为你的问题(无论如何我都能看到)是数字检查代码。

如果 guess 大于 num,则第一个 if 语句将执行,但第二个 if 语句将重定向到 else,从而跳出循环。

您需要使用 elsif。尝试将其更改为这样:

    if guess > num
        print "That's too high. Guess again: "
        guess=gets.to_i
        count+=1
    elsif guess < num
        print "That's too low. Guess again: "
        guess=gets.to_i
        count+=1
    else
        break
    end

I think your problem (the one I can see anyway) is with number checking code.

If guess is greater than num, then the first if statement will execute, but then the second one will redirect to the else, breaking out of the loop.

You need to use an elsif. Try changing it to this:

    if guess > num
        print "That's too high. Guess again: "
        guess=gets.to_i
        count+=1
    elsif guess < num
        print "That's too low. Guess again: "
        guess=gets.to_i
        count+=1
    else
        break
    end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文