# 回数が分かっている繰り返し@ 10.times { print( "やっほ〜 " ) puts( " Yee-ha! " ) } # 回数が分かっている繰り返しA 10.times { |i| print( i , "回目のこんにちは\n" ) } # 回数が分かっている繰り返しB 10.times{ |i| print( 10 ** i , "\n" ) } # 回数が分かっている繰り返しD n=5 n.times {|k| print( " "*(10-k), "*"*k, "\n" ) } # 回数が分かっている繰り返しE n=10 n.times {|k| print( " "*(10-k), "*", " "*(9-k), "\n" ) } # 変数範囲が決まっている繰り返し@ (0..9).each{ |i| print( 10 ** i , "\n" ) } # 変数範囲が決まっている繰り返しA (5..10).each {|i| print( i ) if i%2==0 then print( " は偶数" ) else print( " は奇数" ) end print( "\n" ) } # 変数範囲が決まっている繰り返しB n=gets.chomp.to_i m=gets.chomp.to_i total=0 (n..m).each { |i| total += i } print( n , "から" , m , "までの合計は" , total ) # 変数範囲が決まっている繰り返しD (-10..10).each{ |i| print( i , " " , i**2 , "\n" ) } (10..-10).each{ |i| print( i , " " , i**2 , "\n" ) } # 刻み幅に小数値を使用したい場合 11.times{ |i| print( i / 10.0 , " " , (i/10.0)**2.0 , "\n" ) } (0..10).each{ |i| print( i / 10.0 , " " , (i/10.0)**2.0 , "\n" ) } (0..100).each{ |i| x = i.to_f / 100 print( x, " " , x**2.0 , "\n" ) } # 擬似乱数A 30.times { if rand() > 0.5 then print( "1" ) else print( "0" ) end } # 擬似乱数B s = 0 loop{ s = rand(6)+1 break if s == 6 print( s , "\n" ) }