2017-07-11 2 views
0

Ich mache diesen Code, aber ich kann nicht den richtigen Weg finden, um den Zähler nach der Iteration mit allen Elementen im Array zurückgesetzt.Reset Zähler nach Abschluss der Iteration mit den Array-Elementen

Die Idee ist, dass Sie nach dem letzten Lied in der Playlist in die erste gelangen. Hier ist mein Code, ich würde Ihre Hilfe sehr schätzen. Danke im Voraus!

class Playlist 
    attr_accessor :songs 
    def initialize (name,songs) 
    @name = name 
    @songs = songs 
    end 

    def display_name 
    @name 
    end 

    def number_of_songs 
    "There are #{@songs.count} songs in the #{display_name}" 
    end 

    def add_song(song) 
    @songs << song 
    @songs 
    end 

    def next_song 
    i = 0 
    while i != 9 
     p "Playing song: #{@songs[i]}" 
     p "skip song? (Y/N)" 
     user_input = gets.chomp 
     if user_input.downcase == "y" && i != 8 
     i += 1 
     elsif user_input.downcase == "n" && i != 8 
     puts "Playing song" 
     sleep(1) 
     print'.' 
     sleep(1) 
     print'.' 
     sleep(1) 
     sleep(1) 
     print'.' 
     sleep(1) 
     print'.' 
     sleep(1) 
     sleep(1) 
     print'.' 
     sleep(1) 
     print'.' 
     sleep(1) 
     print'.' 
     puts 
     puts 
     i+=1 
     end 
    end 
    end 
end 

playlist = Playlist.new("The Ultimate Playlist",["In My Life","Naive","She Moves In Her Own Way","Skinny Love","All My Life","All The Small Things","Real"]) 

p playlist.display_name 
p playlist.number_of_songs 
p playlist.add_song("California(Here We Come)") 
puts 
p playlist.next_song 
+1

möchten gefunden Wenn Sie ein Array haben, zB 'a = (1..10) .to_a' statt Iterieren, Sie könnte einfach das Array 'a.rotate!' rotieren und dann immer 'a [0]' abspielen – dawg

Antwort

0

Nachdem ich in die Klasse gegangen bin, habe ich eine Lösung gefunden. das war einfach zu implementieren, aber ich weiß, es gibt bessere Möglichkeiten, um das Problem zu lösen. danke euch allen für die Hilfe! : D

PD: andere Lösungen Kommentar, dass Sie, wenn Sie

class Playlist 
    attr_accessor :songs 
    def initialize (name,songs) 
    @name = name 
    @songs = songs 
    end 

    def display_name 
    @name 
    end 

    def number_of_songs 
    "There are #{@songs.count} songs in the #{display_name}" 
    end 

    def add_song(song) 
    @songs << song 
    @songs 
    end 

    def next_song 
    max [email protected] 
    i = 0 
    finish = "" 
    puts "Write Play to start the playlist (#{@name})" 
    puts 
    promt =">" 
    print promt 
    while finish.downcase != "play" 
     finish = gets.chomp 
     i = 0 
     puts 
    while i <max 
     p "Playing song: #{@songs[i]}" 
     p "skip song? (Y/N) or exit to end the playlist" 
     print promt 
     user_input = gets.chomp 
     if user_input.downcase == "y" && i != 8 
     i += 1 
     elsif user_input.downcase == "n" && i != 8 
     puts "Playing song" 
     sleep(1) 
     print'.' 
     sleep(1) 
     print'.' 
     sleep(1) 
     sleep(1) 
     print'.' 
     sleep(1) 
     print'.' 
     sleep(1) 
     sleep(1) 
     print'.' 
     sleep(1) 
     print'.' 
     sleep(1) 
     print'.' 
     puts 
     puts 
     i+=1 
     elsif user_input.downcase == "exit" 
     exit! 
     end 
    end 
    puts 
    p "Restarting playlist" 
    puts 
    puts "Write Play to start the playlist (#{@name})" 
    puts 
    end 
end 
end 

playlist = Playlist.new("The Ultimate Playlist",["In My Life","Naive","She Moves In Her Own Way","Skinny Love","All My Life","All The Small Things","Real"]) 

p playlist.display_name 
p playlist.number_of_songs 
p playlist.add_song("California(Here We Come)") 
puts 
p playlist.next_song 
Verwandte Themen