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
输入为空时中断?
你的问题是什么?很抱歉让你困惑,我已经更正了问题。谢谢你的帮助。这是有道理的!