我試着去了解如何添加陣列到陣列中,我有以下代碼:增加一個陣列到陣列的Ruby
puts "would you like to save a data set"
response = gets.chomp
if response == "y"
puts "create a new dataset?"
create_data_set = gets.chomp
while create_data_set == "y"
puts "what do you want to name the data set?"
dataset = gets.chomp
dataset = Array.new
puts 'would you like to add some grades to the array?'
store_grades_response = gets.chomp
while store_grades_response == "y"
puts 'enter grade ->'
grade = gets.chomp.to_i
dataset << grade
puts 'would you like to store another grade?'
store_grades_response = gets.chomp
end
all_data_sets = Array.new
all_data_sets.push(dataset)
puts "would you like to create a new data set?"
create_data_set = gets.chomp
end
end
puts all_data_sets
進出口基本要求用戶輸入應創建一個數組一個數組名,給數組添加值,如果用戶需要,可以向數組添加更多的數組和值。最後,數組應該被添加到數組中。然後我試圖顯示所有的數組。
該代碼工作正常,我遍歷一切,但當它puts all_data_sets
它只顯示最後創建的數組?我想所有的數組存儲那叫一個陣列all_data_sets
我在代碼中注意到的東西。在你的第一個'while'循環中,你創建了一個局部變量'dataset',它應該是一個'string',表示用戶想要的**數據集**的名字,但是你將'dataset'重新賦值給一個'array'這基本上意味着用戶選擇的名稱會丟失 –