大年三十。让字母在屏幕上奔跑:(sleep , system"clear")

system "clear",ruby清屏(osk系统上,window上用system "cls")。

https://stackoverflow.com/questions/116593/how-do-you-clear-the-irb-console/116614 

sleep 1 #程序等待1秒。 

让字母在屏幕上奔跑:

用到嵌套循环:

1.外循环用于输出奔跑步数(配合system "clear"和sleep 0.1出现奔跑效果)

2.内循环用于输出在字母H前面打印多少个空格,print " "(体现奔跑的效果)

i = 0
while i <= 100
  system "clear"
  b= 1 #第一次内循环判断flase,不打印空格
  while b <= i
    print " "
    b += 1
  end
  print "H "
  sleep 0.2
  i += 1
end

从右往左跑的小人:

增加头身腿,三行。每行都要有空格的循环配合。 

i = 1
while i <= 20
  system "clear"
  b= 21
  while b >= i
    print " "
    b -= 1
  end
  print " O "
  b = 21
  while b >= i
    print " "
    b -= 1
  end
  print "<H> "
  b = 21
  while b >= i
    print " "
    b -= 1
  end
  print "I I"
  sleep 0.05
  i += 1
end
原文地址:https://www.cnblogs.com/chentianwei/p/8449302.html