【问题标题】:Ruby Code issueRuby代码问题
【发布时间】: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


    【解决方案1】:

    Movie#display 方法更改为:

    def display
      "Name of Movie #{@movie_name} and Rank #{@rank}"
    end
    

    播放列表#play 到:

    def play
      movies = @movies.collect do |movie|
        movie.movie_name
        movie.thumbs_up
      end
      puts "#{@name}'s Playlist:#{movies.join(' and ')}"
    end
    

    【讨论】:

      【解决方案2】:

      在您的play 方法中删除:

      puts @movies
      

      puts movie
      

      【讨论】:

        【解决方案3】:

        在你的每一部电影中

        @movies.each do |movie|
          movie.movie_name
          movie.thumbs_up
          puts movie
         end
        

        将其替换为@movies.each { |movie| movie.display } 这将为您提供

        的输出
        playlist1.play
        #Name of Movie Devil and Rank 10
        #Name of Movie Inception and Rank 3
        

        完整代码:

        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
          @movies.each { |movie| movie.display }
         end
        end
        

        【讨论】:

          【解决方案4】:

          当我们在 play 方法中打印对象时:

          def play
              puts "#{@name}'s Playlist:"
              puts @movies
              @movies.each do |movie|
                   movie.movie_name
                   movie.thumbs_up
                   puts movie
               end
          
          end
          

          上面使用 puts @movies 将打印对象地址,如果我们希望它打印一个字符串,那么我们必须在电影类中重载它。下面是正确的代码:

          class Movie
          
          attr_accessor :movie_name
          attr_reader  :rank
          
          def initialize(movie_name,rank=0)
              @movie_name = movie_name.upcase
              @rank = rank
          end
          def thumbs_up
              @rank = @rank + 1
          end
          
          def thumds_down
              @rank = @rank - 1
          end
          def to_s
          "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.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
          
             Output:
             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
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-07-10
            • 2011-12-13
            • 2012-12-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多