Rubyでプログラミング勉強中(参照)。
Numbers、Letters、Variables and Assignmentと学び、それらをMix Upした内容が、Chapter 4. Mixing It Up。まだまだ基本中の基本中の基本の段階です。以下、この章の練習問題記録。
A Few Things to Try on 4. Mixing It Up
一問目
Write a program which asks for a person’s first name, then middle, then last. Finally, it should greet the person using their full name.
小細工せず、素直に書く。
puts 'What\'s your first name?' fName = gets.chomp puts 'My first name is ' + fName + '.' puts '' puts 'What\'s your middle name?' mName = gets.chomp puts 'My middle name is ' + mName + '.' puts '' puts 'And what\'s your last name?' lName = gets.chomp puts 'My last name is ' + lName + '.' puts '' puts 'Thank you, nice to meet you, ' + 'Mr./Ms. ' + fName + ' ' + mName + ' ' + lName + '.'
二問目
Write a program which asks for a person’s favorite number. Have your program add one to the number, then suggest the result as a bigger and better favorite number. (Do be tactful about it, though.)
これも素直に書く。
puts 'What is your faivorite number?' fNum = gets.chomp puts 'My faivorite number is ' + fNum + '.' puts '' puts 'Hmmm, I think that ' + (fNum.to_i + 1).to_s + ' is more suitable number for you.'
雑感
先にも書いたけど、まだまだ基本中の基本中の基本の段階。つまずく要素も全くなし。それでも結構楽しい。