「プログラミング入門 - Rubyを使って」 Lesson8
プログラミング入門 - Rubyを使って -, by Chris Pine, 日本語ver. by S. Nishiyama
今回はLesson8の「メソッドの作り方」をやっていこうと思います。
練習問題1
• 上のenglishNumberを拡張してみましょう。 最初にthousand(千の桁)を導入してください。上のプログラムでは 'ten hundred'となっているところを'one thousand'を返すように、 あるいは、'one hundred hundred'の代わりに'ten thousand'を 返すようにします。
「プログラミング入門 - Rubyを使って」 Lesson8
実行結果
zero ten eleven one hundred one hundred one two hundred thirty-four one thousand three thousand two hundred eleven five thousand two hundred sixty-eight one thousand thousand one thousand one thousand
練習問題2
• englishNumberにさらに改良を加えましょう。 milion(百万)を加えます。その結果、'one thousand thousand'の代わりに 'one million'が得られるようにします。 その後、billion(十億)とかtrillion(兆)とかを追加していってみましょう。
「プログラミング入門 - Rubyを使って」 Lesson8
実行結果
zero nine ten eleven seventeen thirty-two eighty-eight ninety-nine one hundred one hundred one two hundred thirty-four three thousand two hundred eleven nine hundred ninety-nine thousand nine hundred ninety-nine one trillion
練習問題3
• weddingNumberはどうでしょう? このプログラムはenglishNumber とほとんど同じように動作しますが、"and" という接続詞をやたらめったら挿入します。 たとえば 'nineteen hundred and seventy and two' という具合に、結婚式の 招待状のような感じで。
正直に言うとweddingNumberのことを正確に把握できていないのであっているか定かではありませんが、一応それと思われるものを(笑)
「プログラミング入門 - Rubyを使って」 Lesson8
実行結果
zero nine ten eleven seventeen thirty and two eighty and eight ninety and nine one hundred one hundred and one two hundred and thirty and four three thousand and two hundred and eleven nine hundred and ninety and nine thousand and nine hundred and ninety and nine one trillion
練習問題4
• "99本のビールが壁に..." englishNumberと以前作ったプログラムを使って、今度は正しい 方法で この歌の詩を出力させなさい。 その後は、9999から初めてコンピュータをこらしめましょう。
「プログラミング入門 - Rubyを使って」 Lesson8
実行結果
one thousand Bottles of beer on the wall one thousand Bottles of beer Take one down and pass it around nine hundred ninety-nine Bottles of beer on the wall nine hundred ninety-nine Bottles of beer on the wall nine hundred ninety-nine Bottles of beer Take one down and pass it around nine hundred ninety-eight Bottles of beer on the wall ...中略... two Bottles of beer on the wall two Bottles of beer Take one down and pass it around one Bottles of beer on the wall one Bottles of beer on the wall one Bottles of beer Take one down and pass it around zero Bottles of beer on the wall