我的程序中出现错误“没有将整数隐式转换为字符串”

时间:2019-05-19 03:18:33

标签: ruby

我正在创建一个基于文本的音乐播放器程序。

在read_genre()函数内部,该函数显示可用类型的列表(我在read_genre()函数外部的实例$ genre_names变量数组内定义了9种不同类型),并提示用户为专辑选择一种类型,它会读取用户输入的类型编号。然后从read_album中调用read_genre,并在print_album函数中将其打印出来。

问题是我收到一条错误消息“没有将nill隐式转换为字符串(TypeError)”行134,该行从print_album()函数中放入了Genre is ' + album.genre

$genre_names = ['Null','Pop', 'Classic', 'Jazz', 'Rock', 'KPop', 'Metal', 'Punk', 'Romance', 'Latin']

# Display the genre names in a
# numbered list and ask the user to select one
def read_genre()
    length = $genre_names.length
    index = 0
    print $genre_names
  while (index < length)
    select_genre = read_integer_in_range("Select Genre: ", 0,9)
        case select_genre
        when 1
        puts "#{index + 1} " + $genre_names[1]
        break
        when 2
        puts "#{index + 2 } " + $genre_names[2]
        break
        when 3
        puts "#{index + 3} " + $genre_names[3]
        break
        when 4
        puts "#{index + 4} " + $genre_names[4]
        break
        when 5
        puts "#{index + 5} " + $genre_names[5]
        break
        when 6
        puts "#{index + 6} " + $genre_names[6]
        break
        when 7
        puts "#{index + 7} " + $genre_names[7]
        break
        when 8
        puts "#{index + 8} " + $genre_names[8]
        break
        when 9
        puts "#{index + 9} " + $genre_names[9]
        break
         else
          puts 'Please Select again'
          break 
        end
        index
    end

end

def read_album
    # Complete the missing code
  album_title = read_string("Enter Title: ")
  album_artist = read_string("Enter Artist name: ")
  album_genre = read_genre()
  tracks = [tracks]
  album = Album.new(album_title, album_artist, album_genre, tracks)
  album
end


def print_tracks tracks
    # Complete the missing code
    index = 0
    while (index < tracks.length)
     print_track(tracks[index])
     index += 1
    end
end

1 个答案:

答案 0 :(得分:1)

您在music_file方法中定义了main,但是您试图在另一个未定义的方法中访问它,因此出错。

让我们分析一下您的代码在做什么:

def search_for_track_name(tracks, search_string)
index = 0
count = music_file.gets() 
# Trying to access a variable that is outside of the method's scope
# music_file doesn't exist here.

while (index < count)
    if (tracks[index] == search_string)
        return found_index = -1
        # You are returning -1 if the condition is true, which means
        # returning -1 when the string is found even though you want
        # the exact opposite.
    end
    index += 1
end
 found_index
 # If the pointer gets here, it means that the if statement was never true
 # therefore found_index was never defined

end

让我们解决这些问题:

def search_for_track_name(tracks, search_string)
index = 0
count = tracks.length
# will loop through the array's items

while (index < count)
    if (tracks[index] == search_string)
        return index
        # will return index if found
    end
    index += 1
end
  -1
  # if the loop finishes and nothing is found, will return -1
end

或者您可以只使用index方法来返回数组中元素的第一个匹配项的索引。

tracks.index(search_string)

https://ruby-doc.org/core-2.6.3/Array.html#method-i-index