這是代碼:'蘭特':的範圍內沒有隱式轉換成整數
#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>'
將您的代碼添加到問題中,而不是它的屏幕截圖。 –
以防萬一你不知道:'rand(1..90)'會給你一個介於1和90之間的隨機數。 –
是的,但我需要將它分配給某些東西,以便以後可以打印所有隨機數 – Fin