# プログラムの構造 # coding: Windows-31J print( "一番目の数値を入力してください: " ) line = gets.chop x1 = line.to_f print( "二番目の数値を入力してください: " ) line = gets.chop x2 = line.to_f print( "#{x1} + #{x2} = #{ x1+x2 }" ) # if文の構造 if a > 0 then if a < 10 then print( a ) end end if a > 0 and a < 10 then print( a ) end if a > 0 then if a < 10 then x = a*10 end if a >= 10 then x = a*100 end end if a > 0 then if a < 10 then x = a*10 end end if a > 0 then if a >= 10 then x = a*100 end end # if式@(例) a = 100 # aの値を変えて実行しなさい if a > 0 then if a > 10 then a += 1 end a += 1 end print( a ) # if式A(例) # coding: Windows-31J x=10 if x % 2 == 0 then print( x , "は偶数です" ) else print( x , "は奇数です" ) end # if式A(例) # coding: Windows-31J x=10 if x % 2 != 0 then print( x , "は奇数です" ) else print( x , "は偶数です" ) end # if式A(例) # coding: Windows-31J x=10 y=20 if x >=y then print( x , "は" , y , "以上" ) else print( x , "は" , y , "未満" ) end # if式B(例) # coding: Windows-31J x=10 if x % 2 == 0 then if x >= 10 then print( x , "は偶数で10以上" ) else print( x , "は偶数で10未満" ) end else if x >= 10 then print( x , "は奇数で10以上" ) else print( x , "は奇数で10未満" ) end end # if式B(例) score = 75 grade = if score >= 70 then if score >= 80 then "A" else "B" end else if score >= 60 then "C" else "D" end end print( "Your score #{score} corresponds to #{grade}\n" ) # if式B(例) a = 100 # aの値を変えて実行しなさい if a > 10 then if a > 100 then a += 1 else a -= 1 end else if a > -10 then a += 10 else a -= 10 end end # if式C(例) # coding: Windows-31J x = 10 if x >= 10 then print( x , "は10以上" ) elsif x >= 5 then print( x , "5以上,10未満" ) else print( x , "は5未満" ) end # coding: Windows-31J x = 10 if x >= 10 then print( x , "は10以上" ) elsif x >= 5 then print( x , "は5以上,10未満" ) elsif x >= 0 then print( x , "は0以上,5未満" ) else print( x , "は0未満" ) end # coding: Windows-31J if x < 0 then print( x , "は0未満" ) elsif x < 5 then print( x , "は0以上,5未満" ) elsif x < 10 then print( x , "は5以上,10未満" ) else print( x , "は10以上" ) end # unless文 # coding: Windows-31J x=10 unless x % 2 == 0 then print( x , "は奇数です" ) else print( x , "は偶数です" ) end # case文@ x=10 y=5 opt="+" case opt when "+" then print( x , "+" , y , "=" , x+y ) when "-" then print( x , "-" , y , "=" , x-y ) when "*" then print( x , "*" , y , "=" , x*y ) when "/" then print( x , "/" , y , "=" , x/y ) else print( " x = " , x , " y= " , y ) end # case文A # coding: Windows-31J opt=3 case opt when 1,2,3 puts( "1〜3" ) when 4,5,6 puts( "4〜6" ) when 7,8,9 puts( "7〜9" ) else puts( "10〜" ) end # 無限の繰り返しA # coding: Windows-31J loop{ print( "こんにちは\n" ) } # 無限の繰り返しB # coding: Windows-31J i = 0 loop{ i = i+1 print( i , "回目のこんにちは\n" ) break if i >= 10 } # 無限の繰り返しC i = 0 loop{ print( 10 ** i , "\n" ) break if i >= 10 i = i+1 } # 無限の繰り返しC' i = 5 loop{ print( 10 ** i , "\n" ) break if i >= 10 i = i+1 } # 無限の繰り返しC'' i = 10 loop{ print( 10 ** i , "\n" ) break if i <= 1 i = i-1 } # 無限の繰り返しD i = 1 loop{ if i % 2 == 0 then print( i , "\n" ) end break if i >= 100 i = i+1 } # 無限の繰り返しD' i = 2 loop{ print( i , "\n" ) break if i >= 100 i = i+2 } # 無限の繰り返しD'' i = 1 loop{ print( 2*i , "\n" ) break if i >= 50 i = i+1 } # 無限の繰り返しE # coding: Windows-31J i = 1 total = 0 loop{ total += i break if i >= 100 i = i+1 } print( "合計は" , total ) # 無限の繰り返しE i = 1 total = 0 loop{ total += i break if total > 10000 i = i+1 } print( i ) # 無限の繰り返しE i = 1 total = 1 loop{ total *= i break if total > 10000 i = i+1 } print( i ) # 無限の繰り返しE i = 100 count = 0 loop{ i = i / 2 break if i <= 0 count = count + 1 } print( count ) # 無限の繰り返しのまとめ # coding: Windows-31J i = 0 loop{ print( "やっほ〜 " ) if i>=10 then break end puts( " Yee-ha! " ) i = i+1 } # coding: Windows-31J i = 0 loop{ print( "やっほ〜 " ) break if i>=10 puts( " Yee-ha! " ) i = i+1 }