Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 完成对数组元素的迭代后重置计数器_Arrays_Ruby_Ruby On Rails 3_Loops_Counter - Fatal编程技术网

Arrays 完成对数组元素的迭代后重置计数器

Arrays 完成对数组元素的迭代后重置计数器,arrays,ruby,ruby-on-rails-3,loops,counter,Arrays,Ruby,Ruby On Rails 3,Loops,Counter,我正在编写这段代码,但是在对数组中的所有元素进行迭代之后,我找不到重置计数器的正确方法 这个想法是,在播放列表中的最后一首歌之后,你将进入第一首歌。这是我的代码,我非常感谢你的帮助。提前谢谢 class Playlist attr_accessor :songs def initialize (name,songs) @name = name @songs = songs end def display_name @name end def n

我正在编写这段代码,但是在对数组中的所有元素进行迭代之后,我找不到重置计数器的正确方法

这个想法是,在播放列表中的最后一首歌之后,你将进入第一首歌。这是我的代码,我非常感谢你的帮助。提前谢谢

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
class播放列表
属性存取器:歌曲
def初始化(名称、歌曲)
@name=name
@歌曲
结束
def显示单元名称
@名字
结束
def歌曲的数量
“在#{display_name}中有#{@songs.count}首歌曲”
结束
def添加歌曲(歌曲)

@歌曲上课后,我找到了解决办法。这很容易实现,但我知道有更好的选择来解决这个问题。 谢谢大家的帮助!: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 =@songs.length
    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
class播放列表
属性存取器:歌曲
def初始化(名称、歌曲)
@name=name
@歌曲
结束
def显示单元名称
@名字
结束
def歌曲的数量
“在#{display_name}中有#{@songs.count}首歌曲”
结束
def添加歌曲(歌曲)

@如果你有一个数组,比如说
a=(1..10)。要想让a
代替迭代,你可以旋转数组
a.rotate然后始终播放
a[0]