【发布时间】:2016-09-21 15:18:05
【问题描述】:
下面给出了实现电影播放列表的代码,但这里的问题是当我试图运行这个程序时,它给出的输出如下。请帮助我获得正确的输出
class Movie
attr_accessor :movie_name, :rank
def initialize(movie_name,rank)
@movie_name = movie_name
@rank = rank
end
def thumbs_up
@rank = @rank + 1
display
end
def thumds_down
@rank = @rank - 1
display
end
def display
puts "Name of Movie #{@movie_name} and Rank #{@rank}"
end
end
class Playlist
def initialize(name)
@name = name
@movies = []
end
def add_movies movie
@movies << movie
end
def play
puts "#{@name}'s Playlist:"
puts @movies
@movies.each do |movie|
movie.movie_name
movie.thumbs_up
puts movie
end
end
end
movie1 = Movie.new("Devil",10)
movie2 = Movie.new("Inception",3)
playlist1 = Playlist.new("John")
playlist1.add_movies(movie1)
playlist1.add_movies(movie2)
playlist1.play
输出:
John's Playlist:
Name of Movie Devil and Rank 11
Name of Movie Inception and Rank 4
预期输出:
John's Playlist:
Name of Movie Devil and Rank 10
Name of Movie Inception and Rank 3
Name of Movie Devil and Rank 11
Name of Movie Inception and Rank 4
【问题讨论】:
标签: ruby