# while による繰り返し(例@) # coding: Windows-31J i = 1 while i <= 5 do puts( i.to_s + "回目です。" ) i += 1 end # while による繰り返し(例A) total = 0 i = 1 while i <= 10 do total += i i += 1 end puts( total ) # while による繰り返し(例A') total = 0 i = 1 while total <= 100 do total += i i += 1 end print( i , " " , total ) # while による繰り返し(例B) i = 10 while i != 0 do puts( i ) i -= 1 end i = 10 while i > 0 do puts( i ) i -= 1 end # while による繰り返し(例C) i = 0 while i < 10 do puts( i * 2 ) i += 1 end i = 0 while i <=10 do puts( i * 2 ) i += 1 end # while による繰り返し(例D) i = 1000 while i > 0 do puts( i ) i = i / 2 end i = 2 while i < 100000 do puts( i ) i = i * 2 end # while による繰り返し(例E) # coding: Windows-31J i = 1 while i <= 5 do puts( i.to_s + "回目です。" ) end # times と while の違いB i = 2 while i < 100000 do puts( i ) i = i * 2 end # times と while の違いC i = 1 count = 0 while count != 10 do if i % 2 == 0 then print( i , "\n" ) count += 1 end i += 1 end # times と while の違いD count = 0 while count != 10 do x = rand( 100 ) if x % 2 == 0 then print( x , "\n" ) count += 1 end end # times と while の違いE # coding: Windows-31J s = 0.0 n = 0 while s<10.0 do r = rand() s += r n += 1 end puts( "#{n} 回目で10を越えた" ) # 擬似乱数 # coding: Windows-31J s = 0.0 s2= 0.0 n =10 n.times{ |i| r = rand() s += r s2 += r*r puts( "#{r} は #{i} 番目の乱数です." ) } a = s/n v = (s2 - a*a*n)/(n-1) sd = Math.sqrt( v ) puts( "平均= #{a}, 分散= #{v}, 標準偏差= #{sd}" ) # break文A i = 0 total = 0 while i < 10 do total += i i += 1 break if total > 10 end print( i , " " , total ) # break文B i=0 while true do puts( i ) i=i+1 break if i > 10 end # 無限ループ # coding: Windows-31J while true do puts( "こんにちは" ) end # break文C i = 1000 while true do puts( i ) i = i / 2 break if i <= 0 end # break文D 10.times{ |i| puts( i ) break if i > 5 } (10..100).each{ |i| puts( i ) break if i > 20 } # next@ (0..5).each { |i| if ( i==1 || i==4 ) then next end puts( "iは #{i}" ) } # nextA i=0 while i < 10 do i += 1 if i == 5 then next end puts( i ) end # nextB i=0 while i < 10 do i += 1 if i % 2 == 0 then next end puts( i ) end # 繰り返しのまとめA 10.times{ |i| print( i , "\n" ) } (0..9).each{ |i| print( i , "\n" ) } i = 0 loop{ break if i == 10 print( i , "\n" ) i += 1 } i = 0 while i < 10 do print( i , "\n" ) i += 1 end # 繰り返しのまとめB i = 1 loop{ i = i*2 break if i > 100 print( i , "\n" ) } i = 2 while i < 100 do print( i , "\n" ) i = i*2 end i = 1 while true do i = i*2 break if i > 100 print( i , "\n" ) end # 擬似乱数A 30.times { if rand() > 0.5 then print( "1" ) else print( "0" ) end } 30.times { print rand(2) } 30.times { print( if rand() > 0.5 then "1" else "0" end ) } # 擬似乱数B s = 0 loop{ s = rand(6)+1 print( s , "\n" ) break if s == 6 } # 擬似乱数C 5.times{ s = rand(21)-10 print( s , "\n" ) } # 擬似乱数D 5.times{ s = rand(10)/10.0 print( s , "\n" ) } # 擬似乱数を用いた例@ total = 0 5.times{ x = rand(100) print( x , "\n" ) total += x } print( " 合計は" , total ) # 擬似乱数を用いた例A # coding: Windows-31J max = 0 5.times{ x = rand(100) print( x , "\n" ) if max < x then max = x end } print( " 最大値は" , max ) # 擬似乱数を用いた例B # coding: Windows-31J min = 999 5.times{ x = rand(100) print( x , "\n" ) if min > x then min = x end } print( " 最小値は" , min ) # 擬似乱数を用いた例C # coding: Windows-31J a = 0 b = 0 10000.times{ x = rand(2) if x == 0 then a += 1 else b += 1 end } print( " 0の出現回数 " , a , "回\n" ) print( " 1の出現回数 " , b , "回\n" ) # 擬似乱数を用いた例D # coding: Windows-31J ans=rand(100) loop{ print( "数字を入力して下さい\n" ) input = gets.chomp.to_i if ans == input then print( "正解\n" ) break elsif input > ans then print( "正解はその値よりも小さい\n" ) else print( "正解はその値よりも大きい\n" ) end } # downto@ 10.downto(0){ |x| printf( "x=%2d x^2=%3d\n" , x , x**2 ) } 0.downto(-10){ |x| printf( "x=%3d x^2=%6d\n" , x , x**2 ) } # downtoA total = 0 10.downto(0){ |i| total += i break if total > 20 } # downtoB total = 1 1.upto(10){ |x| total *= x } print( total ) # uptoB 0.upto(10){ |x| print( "x= " , x , "\n" ) } total = 0 0.upto(10){ |x| total += x } print( total ) # stepA 0.step(10,2){ |x| print( x , "\n" ) } 1.step(10,2){ |x| print( x , "\n" ) } # stepB 10.step(0,-2){ |x| print( x , "\n" ) } 9.step(1,-2){ |x| print( x , "\n" ) } # stepC total = 0 1.step(50,2){ |i| total += i } print( total ) # stepD total = 0 49.step(1,-2){ |i| total += i } print( total ) # forループA for x in 1..10 do puts( 2**x ) end # forループB for x in -10..10 do puts( 2**x ) end total = 0 for x in 1..10 do total += x end print( total ) # forループC for x in 0..100 do a = x / 100.0 * Math::PI printf( "%f %f\n" , a , Math.sin( a ) ) end