2015-05-26 55 views
-2

這是代碼:'蘭特':的範圍內沒有隱式轉換成整數

#this is the array that the numbers are stored on and where x is created 
@numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90] 

#getting the lowest number and taking away 1 so it matches the number at the appropiate index in the array 
def get_low() 
    number1 = $stdin.gets.chomp 
    new1 = number1.to_i - 1 
end 

#getting the highest number and taking away 1 so it matches the number at the appropiate index in the array 
def get_high() 
    number2 = $stdin.gets.chomp 
    new2 = number2.to_i - 1 
end 


#these lines ask the user for the highest and lowest possible number, and call the functions needed to create the 10 random numbers 
print "\nNote: limit is between 1 and 90" 
print "\nLowest possible number is.... " 
puts get_low() 
print "\nHighest possible number is.... " 
puts get_high() 
print "\nYour 10 random numbers are now being generated..." 
print "\nYour numbers are: " 
print "\n" 

#using the rand() function to get 10 random numbers between get_low and get_high 
def get_numbers() 
    x = "\"#{@numbers[rand("#{get_low}".."#{get_high}")]}\" , \n \"#{@numbers[rand("#{get_low}".."#{get_high}")]}\" , \n\"#{@numbers[rand("#{get_low}".."#{get_high}")]}\" , \n\"#{@numbers[rand("#{get_low}".."#{get_high}")]}\" , \n \"#{@numbers[rand("#{get_low}".."#{get_high}")]}\" , \n\"#{@numbers[rand("#{get_low}".."#{get_high}")]}\" , \n\"#{@numbers[rand("#{get_low}".."#{get_high}")]}\" , \n\"#{@numbers[rand("#{get_low}".."#{get_high}")]}\" , \n\"#{@numbers[rand("#{get_low}".."#{get_high}")]}\" , \n\"#{@numbers[rand("#{get_low}".."#{get_high}")]}\" " 
end 


puts get_numbers() 

但是當我運行它,我得到約29行以下錯誤:

in 'rand': no implicit conversion of range into integer (TypeError) 
from C:/ruby/ca.rb:29:in 'get_numbers' 
from C:/ruby/ca.rb:33:in '<main>' 
+4

將您的代碼添加到問題中,而不是它的屏幕截圖。 –

+0

以防萬一你不知道:'rand(1..90)'會給你一個介於1和90之間的隨機數。 –

+0

是的,但我需要將它分配給某些東西,以便以後可以打印所有隨機數 – Fin

回答

1

坦率地說,你寫的代碼可以用很多方法改進,我希望分享另一種方法來看問題,看看你是否能夠理解它:

module Config 
    NUMBER_OF_RANDOM_NUMBERS_PER_GAME = 10 
    NUMBER_OF_GAMES = 5 
end 

class RandomTen 
    def initialize 
    user_input 
    end 

    def array 
    Config::NUMBER_OF_RANDOM_NUMBERS_PER_GAME.times.map { rand range } 
    end 

    private 

    def user_input 
    puts 
    puts "Random number range" 
    print "Low : " 
    @low = gets.chomp.to_i 
    print "High: " 
    @high = gets.chomp.to_i 
    end 

    def range 
    @[email protected] 
    end 
end 

class Game 
    def self.play 
    Game.new.run.print 
    end 

    def run 
    @results = collect_games do 
     RandomTen.new.array.sort 
    end 

    self 
    end 

    def print 
    puts 
    puts "Sorted random arrays: " 
    @results.each_with_index do |result, index| 
     puts "Game #{index + 1} : #{result}" 
    end 
    end 

    private 

    def collect_games(&block) 
    Config::NUMBER_OF_GAMES.times.map(&block) 
    end 
end 

Game.play 

# ruby game.rb => 
# 
# Random number range 
# Low : 2 
# High: 9 
# 
# Random number range 
# Low : 1 
# High: 6 
# 
# Random number range 
# Low : 2 
# High: 7 
# 
# Random number range 
# Low : 3 
# High: 7 
# 
# Random number range 
# Low : 3 
# High: 8 
# 
# Sorted random arrays: 
# Game 1 : [2, 3, 5, 6, 6, 6, 8, 8, 8, 9] 
# Game 2 : [1, 1, 1, 2, 3, 5, 5, 5, 5, 5] 
# Game 3 : [3, 3, 4, 4, 5, 5, 5, 6, 6, 7] 
# Game 4 : [3, 3, 3, 3, 4, 4, 5, 5, 6, 7] 
# Game 5 : [3, 3, 4, 6, 6, 7, 8, 8, 8, 8] 

注意:

  • 我避免限制隨機數的限制,因爲我相信你可能已經這樣做了,因爲你創建數組的方式。
+0

它的工作:)但有一個新的數組已存儲...如果是的話是什麼?我需要存儲所有的數字,所以當他們完成所有的數字可以排序和打印 – Fin

+0

我沒有完全得到你的要求是什麼?你的意思是你想將數組寫入文件? –

+0

換句話說,我可以將所有隨機數存儲在另一個數組中,因此當用戶決定時,他們可以查看所有隨機數在整個過程中所有的隨機數。這些數字可以分類嗎? – Fin

0

(I目前無法訪問Ruby解釋器)

它可能因爲您將字符串的範圍傳遞給rand方法而失敗,但您應該傳遞整數範圍。從範圍中刪除「,它應該工作

+0

我會盡力謝謝 – Fin

+0

你的意思是在每個#之前和結束之後刪除\'因爲我試過了,它不起作用 – Fin

+0

我這意味着'@numbers [rand(#{get_low} ..#{get_high})]''而不是'@numbers [rand(「#{get_low}」..「#{get_high}」)]' – djaszczurowski

0

它看起來像你想的低輸入和輸入的高之間的隨機數。我想嘗試get_low加蘭特(get_high)。

報告的問題是,這rand函數想要一個int,並且你當前正在傳遞一個範圍(a .. b)

+0

我做了這個:x =「@ numbers [#{get_low} + rand(#{get_high })]「並且它提出了:@numbers [-1 + rand(-1)]而不是10個隨機數 – Fin

+0

您是否爲低和高輸入了零?用代碼減1,這將解釋-1。如何刪除雙引號? – donjuedo

+0

我做到了這一點,它提出了關於'rand'中第27行的錯誤:沒有將字符串隱式轉換爲整數 – Fin

相關問題