2016-10-09 45 views
1

我試着去了解如何添加陣列到陣列中,我有以下代碼:增加一個陣列到陣列的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

+0

我在代碼中注意到的東西。在你的第一個'while'循環中,你創建了一個局部變量'dataset',它應該是一個'string',表示用戶想要的**數據集**的名字,但是你將'dataset'重新賦值給一個'array'這基本上意味着用戶選擇的名稱會丟失 –

回答

1

的問題是,你在每一個循環的末尾創建一個新的陣列all_data_sets。一種解決方案將是在循環之前進行。

puts "would you like to save a data set" 
response = gets.chomp 
all_data_sets = [] 
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 << dataset 
    puts "would you like to create a new data set?" 
    create_data_set = gets.chomp 
    end 
end 

puts all_data_sets 

這樣一來,你推存datasetsall_data_sets每個循環之後。

我希望這足以說明問題。

-1

可以Concat的,前置或後置陣列就這樣

dataset.concat all_dataset 
dataset + all_dataset 

Concat documentation

前置或後內

dataset.push(*all_dataset) 
all_dataset.unshift(*dataset) 

Array stuffs

你也可以做切片和一大堆東西的檢查在Ruby文檔鏈接

0

修復

它,因爲你創造new_data_sets每次你做的循環時間序列,其聲明外包圍while循環

代碼

def main 
    mainDataSet = [] # All datasets 
    dataSetNames = [] # Incase you want to store data set names 

    response = getInput("Would you like to save a data set") 

    if(response == "y") 
     choice = getInput("Create a new dataset?")   
     while choice == "y" 
      dataset = getInput("What do you want to name the data set?") 
      dataSetNames << dataset 
      dataset = [] 
      choice_2 = getInput("would you like to add some grades to the array?") 
      while choice_2== "y" 
       grade = getInput("Enter grade") 
       dataset << grade 
       choice_2 = getInput("Store another grade?") 
      end 
      mainDataSet << dataset 

      choice = getInput("Create a new data set?") 
     end 
    end 

    puts mainDataSet 
    puts dataSetNames 
end 


def getInput(message) 
    puts "#{message} -> " 
    gets.chomp 
end 

希望這有助於。