Ruby 学习'while`/'till`语句

Ruby 学习'while`/'till`语句,ruby,loops,while-loop,Ruby,Loops,While Loop,我试图使用while/until返回短语Wig-Wam四次。我如何描述四次后满足的条件,然后返回 counter = 6 loop do counter = counter + 1 puts "Wig Wam" if counter >= 10 break end end 同样的想法,使用while counter = 6 while counter < 10 counter = counter + 1 puts "Wig Wam" end

我试图使用
while/until
返回短语
Wig-Wam
四次。我如何描述四次后满足的条件,然后返回

counter = 6
loop do
  counter = counter + 1
  puts "Wig Wam"
  if counter >= 10
    break
  end
end

同样的想法,使用while

counter = 6

while counter < 10 
 counter = counter + 1
 puts "Wig Wam"    
end

这一切都与你使用什么样的逻辑比较有关。使用
while
时,您需要使用与主要问题无关的
小技巧,
counter+=1
在Ruby中是
counter=counter+1
的简写,正如您可能知道的,Ruby方法是使用带有块的枚举器:
4.times{puts“Wig Wam”}
counter = 6

begin 
 counter = counter + 1
 puts "Wig Wam"    
end while counter < 10 
4.times { puts "Wig Wham" }
while counter <= 4
  puts 'Wig Wam'
  counter += 1
end

until counter == 4
  puts 'Wig Wam'
  counter += 1
end