我的程序旨在从.txt文件中获取“专辑标题”,“专辑艺术家”,“专辑类型”和“曲目”。它将这些磁道放入一个数组,然后将该数组打印到终端。
一切正常,除了要打印的数组为空“ []”
Ruby代码
module Genre
POP, CLASSIC, JAZZ, ROCK = *1..4
end
$genre_names = ['Null', 'Pop', 'Classic', 'Jazz', 'Rock']
class Album
# NB: you will need to add tracks to the following and the initialize()
attr_accessor :title, :artist, :genre, :tracks
# complete the missing code:
def initialize (title, artist, genre, tracks)
@title = title
@artist = artist
@genre = genre
@tracks = tracks
end
end
class Track
attr_accessor :name, :location
def initialize (name, location)
@name = name
@location = location
end
end
# Returns an array of tracks read from the given file
def read_tracks(music_file)
tracks = Array.new
count = music_file.gets().to_i
index = 0
while index < count
track = read_track(music_file)
tracks << track
index = index + 1
end
tracks
end
# Reads in and returns a single track from the given file
def read_track(music_file)
track_name = music_file.gets
track_location = music_file.gets
track = Track.new(track_name, track_location)
end
# Takes an array of tracks and prints them to the terminal
def print_tracks(tracks)
index = 0
while (index < tracks.length)
puts 'Track Number ' + index.to_s + ' is:'
print_track(tracks[index])
index = index + 1
end
tracks
end
# Reads in and returns a single album from the given file, with all its tracks
def read_album(music_file)
# read in all the Album's fields/attributes including all the tracks
# complete the missing code
album_artist = music_file.gets
album_title = music_file.gets
album_genre = music_file.gets
tracks = read_tracks(music_file)
album = Album.new(album_title, album_artist, album_genre, tracks)
album
end
# Takes a single album and prints it to the terminal along with all its tracks
def print_album(album, tracks)
# print out all the albums fields/attributes
# Complete the missing code.
puts 'Album title is ' + album.title.to_s
puts 'Album artist is ' + album.artist.to_s
puts 'Genre is ' + album.genre.to_s
puts $genre_names[album.genre.to_i]
# print out the tracks
puts 'Tracks are: ' + print_tracks(tracks).to_s
end
# Takes a single track and prints it to the terminal
def print_track(tracks)
tracks.each do |track|
puts('Track title is: ' + track.name.to_s)
puts('Track file location is: ' + track.location.to_s)
end
end
# Reads in an album from a file and then print the album to the terminal
def main
music_file = File.new("album.txt", "r")
if music_file
album = read_album(music_file)
tracks = read_tracks(music_file)
music_file.close
else
puts "unable to read"
end
print_album(album, tracks)
end
main
.txt文件包含的内容
Neil Diamond
Greatest Hits
1
3
Crackling Rose
sounds/01-Cracklin-rose.wav
Soolaimon
sounds/06-Soolaimon.wav
Sweet Caroline
sounds/20-Sweet_Caroline.wav
我收到以下消息:
Album title is Greatest Hits
Album artist is Neil Diamond
Genre is 1
Pop
Tracks are: []
tracks数组何时应打印出不同的轨道,它们的名称和文件位置
答案 0 :(得分:0)
这里是您的解决方案)))这是一些小错误。首先,您有空轨道,因为您读取了文件的结尾。您在VAR=MYDEV
MYDEV_init=UART_init
define create_kernels_c
echo GGGGG $(VAR)
baseinit=$$(echo $(VAR)_init);
$$(eval devinit=\$$baseinit);
echo devinit $$devinit;
endef
all:
@$(call create_kernels_c)
中被第一次叫read_tracks
,在read_album
中被第二次叫。
另外,当您在main
中尝试调用print_track
对象上的循环时,您会遇到一个例外。
Track