-1
wordList = ['1930', '1931', '1932', '1933', '1934', '1935', '1936', '1937', '1938', '1939', '1940']
wordLen = wordList.length
wordRand = rand(wordLen)
year = wordList[wordRand]
非常喜欢这里......福利局的year
的行为是这样的,我每次运行程序时,它选择从wordList
一个随机字符串。问题是,它需要特定的随机选择的号码,并将其设置为等于year
。因此,对于程序的每个实例,每次调用它时,year
与列表中的字符串都是相同的。我怎样才能让它每次选择一个不同的数字?红宝石 - 随机数是相同的随机数,每次
puts 'Why hello there, dear! Grandma is, SO, happy to see you!'
response = gets.chomp
while response != 'BYE'
if response == response.upcase
puts 'NO, NOT SINCE ' + year + '!'
response = gets.chomp
else
puts 'WHAT? SPEAK UP, SONNY!'
response = gets.chomp
end
end
if response == 'BYE'
puts 'OKAY, BYE DEAR!!'
end
编辑:添加背景
不,它不是..对我来说 –
嗯...好吧让我给你看看上下文: –
上面的代码工作正常。下面的代码引用变量'year',所以如果你重新运行下面的代码,年份不会改变。这是你的问题吗?如果是这样,你需要将上面的代码转换成方法'year()'(即'def year()... end')。注意,一个Ruby约定是变量(和方法)名称中的所有字母都是小写字母,并且为了便于阅读而插入了下划线。骆驼案例('LikeThis')被保留用于类和模块名称。 –