首页 > 代码库 > ruby各种循环输出数组元素
ruby各种循环输出数组元素
array = (1..10).to_a# 方法1length = array.length length.times do |t| print "#{array[t]} " end puts "\n"# 方法2length = array.length-1 for i in 0..length do print "#{array[i]} " endputs "\n"# 方法3for i in array do print "#{i} " end puts "\n"# 方法4array.each{|x| print x," "}puts "\n"# 方法5length = array.length i = 0 while i< length do print "#{array[i]} " i = i+1 end puts "\n"# 方法6length = array.length i = 0 until i==length do print "#{array[i]} " i += 1 end puts "\n"# 方法7array.each_index do |i| print "#{array[i]} " end puts "\n"# 方法8length = array.length - 1 0.upto(length) do |i| print "#{array[i]} " endputs "\n"# 方法9length = array.length - 1 i = 0 loop do print "#{array[i]} " i += 1 break if i > length #也可以用break unless i <= length end
ruby各种循环输出数组元素
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。