-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

编辑:添加上下文

4

1 回答 1

1

您似乎只为 生成一个值year,然后在循环中重复使用它。如果您想要不同的号码,请将调用放入rand循环中。

于 2013-10-26T18:09:11.497 回答