0
我試圖在一行上打印幾個數組。第一行是確定的,但是之後的循環會崩潰。我試過了一遍,但沒有雪茄。下面的代碼:在一行中打印多個陣列
chapter_num = ['Chapter 1', 'Chapter 2', 'Chapter 3']
chapter_title = ['Numbers', 'Letters', 'Variables']
chapter_page = ['page 1', 'page 72', 'page 118']
#Prints chapter number
chapter_num.each do |num|
print (num.ljust 0)
#Prints title
chapter_title.each do |title|
print (title.center 14)
#Prints page number
chapter_page.each do |page|
puts (page.rjust 40)
end
end
end
當我運行上面的代碼,它看起來是這樣的:
Chapter 1 Numbers page 1
page 72
page 118
Letters page 1
page 72
page 118
Variables page 1
page 72
page 118
Chapter 2 Numbers page 1
page 72
page 118
Letters page 1
page 72
page 118
Variables page 1
page 72
page 118
Chapter 3 Numbers page 1
page 72
page 118
Letters page 1
page 72
page 118
Variables page 1
page 72
page 118
它想成這樣:
Chapter 1 Numbers page 1
Chapter 2 Letters page 72
Chapter 3 Variables page 118