Ruby脚本需要修复

时间:2014-02-19 07:00:36

标签: ruby

我的ruby脚本有问题。如果有人可以提供帮助,我真的很感激。问题是数字卡在1-2之间;其中2太高而1太低。猜测应该只是整数。

#!/usr/bin/ruby
def highLow(max)
again = "yes"
while again == "yes"
puts "Welcome to the High Low game"
            playGame(max)
            print "Would you like to play again? (yes/no): "
            again = STDIN.gets.chomp

            if again == 'no'
               puts "Have a nice day, Goodbye"

            end
     end
end
#This method contains the logic for a single game and call the feedback method.
def playGame(max)
puts "The game gets played now"


puts "I am thinking of a number between 1 and #{max}." #It show what chosen by user
randomNumber = rand(max)+ 1
print "Make your guess: "
guess = STDIN.gets.chomp

feedback(guess, randomNumber)
end 
#Start while loop
#Logic for feedback method. It's ganna check the guess if it's high or low.
def feedback(guess, randomNumber)
count = 1
while guess.to_i != randomNumber
    count = count + 1
    if guess.to_i < randomNumber
        print "That's too low. Guess again: "
    else
        print "That's too high. Guess again: "
    end
    guess = STDIN.gets.chomp
end
puts "Correct! You guessed the answer in #{count} tries!"

end

highLow(ARGV[0])

1 个答案:

答案 0 :(得分:1)

将您的最后一行更改为:

highLow(ARGV[0].to_i)

ARGV数组包含所有传入的参数 strings ,因此您必须将其强制转换为整数。

相关问题