无法从文件中提取信息

用户名

我的程序旨在从.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数组应打印出不同的轨道,它们的名称和文件位置时

机器人

这是您的解决方案)))这是一些小错误。首先,您有空轨道,因为您读取了文件的结尾。你们已被要求read_tracks在第一时间read_album和第二时间main另外,print_track尝试调用Track对象上的循环时会遇到异常

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)
    @title = title
    @artist = artist
    @genre = genre
  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
    tracks << read_track(music_file)
    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 += 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
  album = Album.new(album_title, album_artist, album_genre)
  album
end


# Takes a single album and prints it to the terminal along with all its tracks
def print_album(album)

  # 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(album.tracks).to_s
end

# Takes a single track and prints it to the terminal
def print_track(track)
  puts('Track title is: ' + track.name.to_s)
  puts('Track file location is: ' + track.location.to_s)
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)
    album.tracks = read_tracks(music_file)
    music_file.close
  else
    puts "unable to read"
  end

  print_album(album)

end

main

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章