2017-07-11 11 views
0

このコードを実行していますが、配列内のすべての要素を反復処理した後にカウンタをリセットする適切な方法が見つかりません。配列の要素を反復処理した後にカウンタをリセットする

プレイリストの最後の曲に移動した後、最初の曲に移動します。ここに私のコードは、私は本当にこれであなたの助けに感謝します。前もって感謝します!

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

あなたは配列を持っている場合は、 '=(1..10)を言うしたい場合は、見つかった他のソリューションをコメント.to_a'代わりに反復の、あなた配列 'a.rotate! 'を回転させるだけで、常に' a [0] 'を再生することができます – dawg

答えて

0

これを解決する方法を見つけた後、私はクラスiに行きました。それはまだ実装するのは簡単だった私は問題を解決するためのより良いオプションがあることを知っている。 ご協力いただきありがとうございます! :D

PD:あなたがし

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 
関連する問題