Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何使用get.chomp填充数组_Ruby_Arrays - Fatal编程技术网

Ruby 如何使用get.chomp填充数组

Ruby 如何使用get.chomp填充数组,ruby,arrays,Ruby,Arrays,以下是我目前掌握的情况: #Table of Contents using an array title = "Table of Consents" #Needs chapters inputted chapters = Array.new puts "Please input chapter names." while gets.chomp != "" chapter = gets.chomp chapters.push chapter break if gets.chomp

以下是我目前掌握的情况:

#Table of Contents using an array
title = "Table of Consents"

#Needs chapters inputted
chapters = Array.new

puts "Please input chapter names."

while gets.chomp != ""
  chapter = gets.chomp
  chapters.push chapter
  break if gets.chomp.empty?
end

#Needs corresponding page numbers inputted
pagenumbers = Array.new

puts "Please input corresponding page numbers."

while gets.chomp != ""
  pagenum = gets.chomp
  pagenumbers.push pagenum
  break if gets.chomp.empty?        
end

puts chapters
puts page numbers

我正在尝试获取
get.chomp
以继续添加到每个数组。当我按下
chapter
pagenum
时,我只得到最后一个字符串/整数输入。如何获取每个输入以填充
章节
pagenum
数组?

这里的问题是,每次在程序中编写
get
时,您都在调用
get
方法,该方法从用户处获取一行输入。您需要将该行存储在某个位置,以便以后使用。试着这样做:

chapters = []
while true
  input = gets.chomp
  break if input.empty?
  chapters << input
end
puts "Chapters: " + chapters.join(", ")
章节=[]
虽然是真的
input=gets.chomp
输入为空时中断?

你的问题是什么?很抱歉让你困惑,我已经更正了问题。谢谢你的帮助。这是有道理的!