我要求用户输入一个数字并根据该数字我想添加某些玩家到我的游戏。如何根据用户输入添加到Ruby数组?
class Player
def players_playing
players = []
puts('How many players are playing?')
players_amount = gets.chomp
for i in range(players_amount)
puts ('What is the players name')
name = gets.chomp
players.push(name)
end
end
end
所以,如果他们输入3.然后代码应循环3次,并要求用户的名字。例如
What is the players name? Rich
What is the players name? Tom
What is the players name? Charles
话,那就得玩家= [ '富', '汤姆', '查尔斯']
任何想法,为什么我的代码是不正确的? (我想,这是与range
部分也许做)
'range'定义在哪里? – Stefan