2014-02-19 197 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