如何将此while
循环转换为for
循环?
ten_things = "Apples Oranges Crows Telephone Light Sugar"
stuff = ten_things.split(' ')
puts "Wait there are not 10 things in that list. Let's fix that."
more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]
while stuff.length != 10
next_one = more_stuff.pop
puts "Adding: #{next_one}"
stuff.push(next_one)
puts "There are #{stuff.length} items now."
end
我尝试过无限循环
答案 0 :(得分:2)
我不明白为什么如果TEncoding.GetEncoding(1251)
或for
循环是针对它的话,你会为while
循环而烦恼。
然而,这将有效:
until
答案 1 :(得分:2)
您可以使用
array_name.each { |item|
break if stuff.length > 10
stuff.push(item)
}