7章 流れ制御なる章立てに於ける練習問題にて正解の掲載されぬ本誌を購読の上、 少しく不安を感じたかも知れぬとなればご同志の為の、 参照、比較検討用に供す個人的な解釈、解答でした。
7.1 比較メソッド
7.2 分岐
7.3 ループ
7.4 論理を少し
7.5 練習問題
2006年9~10月当時、 上記アーティクルでご紹介以外にも、 ひとつ類似コードファイルの残り居れば、 今回は其れを贄に供したいと思います。
スポンサーリンク
7.5.1. 99本のビールが壁ににて、得られる結果は前回と同様、遠足などでよく歌われる古典的な童謡の歌詞、「 99 Bottles of Beer on the Wall 」を出力するプログラムを書いてみましょう。
Please, enter your favorite Number!
5
5 Bottles of beer on the wall
5 Bottles of beer
Take one down and pass it around
4 Bottles of beer on the wall
(ry
No Bottles of beer on the wall
No Bottles of beer
Take one down and pass it around
No Bottles of beer on the wall
なることを見当に以下を記述しました。
5
5 Bottles of beer on the wall
5 Bottles of beer
Take one down and pass it around
4 Bottles of beer on the wall
(ry
No Bottles of beer on the wall
No Bottles of beer
Take one down and pass it around
No Bottles of beer on the wall
def bottles_of_beer request
if request == 0
puts 'No Bottles of beer on the wall'
puts 'No Bottles of beer'
puts 'Take one down and pass it around'
puts 'No Bottles of beer on the wall'
elsif request == 1
puts request.to_s + ' Bottle of beer on the wall'
puts request.to_s + ' Bottle of beer'
puts 'Take one down and pass it around'
puts 'No Bottles of beer on the wall'
puts
bottles_of_beer request-1
else
puts request.to_s + ' Bottles of beer on the wall'
puts request.to_s + ' Bottles of beer'
puts 'Take one down and pass it around'
puts (request-1).to_s + ' Bottles of beer on the wall'
puts
bottles_of_beer request-1
end
end
puts 'Please, enter your favorite Number!'
request = gets.chomp.to_i
flag = false
while flag == false
if request < 0
puts 'Please, enter plus Number!'
request = gets.chomp.to_i
elsif request > 15
puts 'Please,enter Number that is smaller than 15'
request = gets.chomp.to_i
else
bottles_of_beer request
flag =true
end
end
ユーザーに繰り返し回数を要求するメイン部分は前回から其の儘に、
関数部
bottles_of_beer
に於いて
while
の代わりに再帰的手法を用いようと、
大凡、下記の赤字取り消し線部分を緑字部分に置き換えています。
if request == 0
puts 'No Bottles of beer on the wall'
puts 'No Bottles of beer'
puts 'Take one down and pass it around'
puts 'No Bottles of beer on the wall'
elsif request == 1
puts request.to_s + ' Bottle of beer on the wall'
puts request.to_s + ' Bottle of beer'
puts 'Take one down and pass it around'
puts 'No Bottles of beer on the wall'
puts
bottles_of_beer request-1
else
puts request.to_s + ' Bottles of beer on the wall'
puts request.to_s + ' Bottles of beer'
puts 'Take one down and pass it around'
puts (request-1).to_s + ' Bottles of beer on the wall'
puts
bottles_of_beer request-1
end
end
puts 'Please, enter your favorite Number!'
request = gets.chomp.to_i
flag = false
while flag == false
if request < 0
puts 'Please, enter plus Number!'
request = gets.chomp.to_i
elsif request > 15
puts 'Please,enter Number that is smaller than 15'
request = gets.chomp.to_i
else
bottles_of_beer request
flag =true
end
end
def bottles_of_beer request
while request >= 0
if request == 0
puts 'No Bottles of beer on the wall'
puts 'No Bottles of beer'
puts 'Take one down and pass it around'
puts 'No Bottles of beer on the wall'
request -= 1
elsif request == 1
puts request.to_s + ' Bottle of beer on the wall'
puts request.to_s + ' Bottle of beer'
puts 'Take one down and pass it around'
request -= 1
puts 'No Bottles of beer on the wall'
puts
bottles_of_beer request-1 #===>再帰
else
puts request.to_s + ' Bottles of beer on the wall'
puts request.to_s + ' Bottles of beer'
puts 'Take one down and pass it around'
request -= 1
puts (request-1).to_s + ' Bottles of beer on the wall'
puts
bottles_of_beer request-1 #===>再帰
end
end
end
現在の手元環境
while request >= 0
if request == 0
puts 'No Bottles of beer on the wall'
puts 'No Bottles of beer'
puts 'Take one down and pass it around'
puts 'No Bottles of beer on the wall'
request -= 1
elsif request == 1
puts request.to_s + ' Bottle of beer on the wall'
puts request.to_s + ' Bottle of beer'
puts 'Take one down and pass it around'
request -= 1
puts 'No Bottles of beer on the wall'
puts
bottles_of_beer request-1 #===>再帰
else
puts request.to_s + ' Bottles of beer on the wall'
puts request.to_s + ' Bottles of beer'
puts 'Take one down and pass it around'
request -= 1
puts (request-1).to_s + ' Bottles of beer on the wall'
puts
bottles_of_beer request-1 #===>再帰
end
end
end
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
で走らせても、
問題無く想定通りの結果を得られました。
初めてのプログラミング4(閏年)
初めてのプログラミング当ブログに於いてRuby及びプログラミングの入門書初めてのプログラミングの各章の最後に位置付けられる練習問題にて正解の掲載されぬに少しく不安を感じたかも知れぬ本誌購読のご同志の為の、参照、比較検討用に生贄的に供す個人的な解釈、解答を掲載