该程序旨在从.txt文件中读取专辑信息,一旦用户输入了所需的曲目,该程序就会检查数组以查看其是否包含该曲目。我已经成功地将曲目读取到阵列中,但是我的程序无法识别字符串是否位于该阵列中。
Ruby文件
require './input_functions'
module Genre
POP, CLASSIC, JAZZ, ROCK = *1..4
end
$genre_names = ['Null', 'Pop', 'Classic', 'Jazz', 'Rock']
class Album
attr_accessor :title, :artist, :genre, :tracks
def initialize (title, artist, genre)
# insert lines here
@title = title
@artist = artist
@genre = genre
end
end
class Track
attr_accessor :name, :location
def initialize (name, location)
@name = name
@location = location
end
end
def read_track(music_file)
track_name = music_file.gets
track_location = music_file.gets
track = Track.new(track_name, track_location)
end
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
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
def read_album music_file
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
def print_album album
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
def print_track track
puts('Track title is: ' + track.name.to_s)
puts('Track file location is: ' + track.location.to_s)
end
** This is where I am having my problem
# search for track by name.
# Returns the index of the track or -1 if not found
def search_for_track_name(tracks, search_string)
search_string = gets.chomp
index = 0
while (index < tracks.length)
tracks.include?(search_string)
index = index + 1
end
if tracks.include?(search_string)
return index
else
index = -1
end
return index
end
# Reads in an Album from a file and then prints all 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()
end
search_string = read_string("Enter the track name you wish to find: ")
index = search_for_track_name(album.tracks, search_string)
if index.to_i > -1
puts "Found " + album.tracks[index].name + " at " + index.to_s
else
puts "Entry not Found"
end
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
当用户输入“ Sweet Caroline”时,程序应返回找到曲目的提示,而不是:
Enter the track name you wish to find:
Sweet Caroline
Entry not Found
Tracks are [#<Track:0x000000000331be58 @name="Crackling Rose\n", @location="sounds/01-Cracklin-rose.wav\n">, #<Track:0x000000000331bdb8 @name="Soolaimon\n", @location="sounds/06-Soolaimon.wav\n">, #<Track:0x000000000331bd18 @name="Sweet Caroline\n", @location="sounds/20-Sweet_Caroline.wav\n">]
答案 0 :(得分:1)
条件if tracks.include?(search_string)
永远不会是true
,因为tracks
是Track
实例的数组。因此,该数组不包含特定的字符串。
它可能包含名称匹配的曲目。查找具有匹配名称Array#index
的曲目的索引可能会有所帮助:
tracks.index { |track| track.name == search_string }
由于index
返回nil
(如果找不到),您可以简化整个if...else
的块,将整个search_for_track_name
方法的问题简化为这一行:
tracks.index { |track| track.name == search_string } || -1
答案 1 :(得分:0)
我注意到您的搜索循环无效。它检查N次,如果数组中包含搜索字符串,则忽略该检查的结果,并将其添加到索引中。因此索引始终为N(在您的情况下为3)。然后再次检查 数组是否包含搜索字符串,否则返回-1。因此,您的方法将始终返回N或-1。
幸运的是,Ruby是一种语言,已经为您完成了许多繁重的工作。坦率地说,我认为您想要:
def search_for_track_name(tracks, search_string)
tracks.index(search_string) || -1
end
如果找不到搜索字符串, #index
将返回nil
;这对于Ruby来说是更标准的,但是由于您的要求是返回-1,因此|| -1
会为您添加它。 ({nil
和false
是Ruby中 only 的伪造值,因此,如果第一部分是{{ 1}}。)
我也注意到了其他事情,我认为这可能是问题所在。您是否有机会必须按两次 Enter ?因为来自||
的第一个输入将被nil
覆盖。在将read_string
传递给方法之前,您已经设置了它...
我注意到的第三件事-您的曲目名称填充了换行符(search_string = gets.chomp
)。您还需要search_string
,或者遵循其他用户的建议,寻找部分匹配而不是完全匹配。