我无法理解这段代码是如何工作的,该程序能够读取文件,但最终多次打印同一行。
输出:
Track title is: c/music/1
Track file location is: c/music/1
Track title is: c/music/2
Track file location is: c/music/2
Track title is: c/music/3
Track file location is: c/music/3
预期输出:
Track title is: Taco
Track file location is: c/music/1
Track title is: Burrito
Track file location is: c/music/2
Track title is: Nacho
Track file location is: c/music/3
代码:
class Track
attr_accessor :title, :file_location
end
def read_tracks music_file
count = music_file.gets().to_i
tracks = Array.new
$i = 0
while $i < count do
track = read_track(music_file)
tracks << track
$i += 1
end
tracks
end
def read_track aFile
track_title = aFile.gets
track_file_location = aFile.gets
track = Track.new
track.title = track_title
track.file_location = track_file_location
end
def print_tracks tracks
$i = 0
while $i < tracks.length do
print_track(tracks)
$i += 1
end
end
def print_track tracks
puts('Track title is: ' + tracks[$i].to_s)
puts('Track file location is: ' + tracks[$i].to_s)
end
def main
aFile = File.new("input.txt", "r")
if aFile
tracks = read_tracks(aFile)
aFile.close
else
puts "Unable to open file to read!"
end
print_tracks(tracks)
end
main
输入文件示例:
5
Taco
c/music/1
Burrito
c/music/2
Nacho
c/music/3
答案 0 :(得分:1)
尝试一下,将行处理为数组,然后对其进行处理:
lines = File.readlines('tracks.txt') # reads lines into array
lines.reject! { |e| e == "\n" } # removes empti lines
total_tracks = lines.shift.chomp.to_i # extract the first line from the array
lines.each_slice(2) { |e| puts e } # lines now contains only the pair track/directory
# adapt at your will
有关each_slice(2)
的信息,请参见Enumerable#each_slice,它将数组中的元素分组。
答案 1 :(得分:0)
问题出在方法 print_tracks 和 print_track 中。
这些方法应如下所示:
def print_tracks tracks
$i = 0
while $i < tracks.length do
print_track(tracks[$i])
$i += 1
end
end
def print_track track
puts('Track title is: ' + track.title.to_s)
puts('Track file location is: ' + track.file_location.to_s)
end
但是,如果您想使代码更好,请尝试如下操作:
def print_tracks(tracks)
tracks.each do |track|
puts "Track title is: #{track.title}"
puts "Track file location is: #{track.file_location}"
puts
end
end
在这种情况下,整个代码将是:
class Track
attr_accessor :title, :file_location
end
def read_tracks music_file
count = music_file.gets().to_i
tracks = Array.new
i = 0
while i < count do
track = read_track(music_file)
tracks << track
i += 1
end
tracks
end
def read_track aFile
track = Track.new
track.title = aFile.gets
track.file_location = aFile.gets
track
end
def print_tracks(tracks)
tracks.each do |track|
puts "Track title is: #{track.title}"
puts "Track file location is: #{track.file_location}"
puts
end
end
def main
aFile = File.new("input.txt", "r").
if aFile..
tracks = read_tracks(aFile)
aFile.close
else
puts "Unable to open file to read!"
end
print_tracks(tracks)
end
main
我已经使用示例文件input.txt测试了此代码:
3
Taco
c/music/1
Burrito
c/music/2
Nacho
c/music/3
我有输出:
Track title is: Taco
Track file location is: c/music/1
Track title is: Burrito
Track file location is: c/music/2
Track title is: Nacho
Track file location is: c/music/3
这正是您所期望的!