2014-02-19 148 views
0

我遇到了ruby脚本的问题。如果有人能帮忙,我会很感激。问题在于数量在1-2之间;其中2太高而1太低。猜测应该只是整数。Ruby脚本需要修复

#!/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]) 
+0

使用'rand(1..max)' – devanand

+0

我改变了它。现在我得到4个错误。 line 38 line 29 line 8 line 49 – D7m

回答

1

你的最后一行改成这样:

highLow(ARGV[0].to_i) 

ARGV数组包含所有的参数传递为,所以你必须将它转换为整数。

+0

我还是有同样的问题!当我猜测1它太低,2太高! 我是否必须仅在最后一行修复ARVG?或者我应该把它放在另一行? 谢谢 – D7m