Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Flow Control - Branching & Looping - WORKING PROGRAM EXAMPLES
- # "99 bottles of beer on the wall..." Write a program which prints out the lyrics to that beloved classic, that field-trip favoriteff: "99 Bottles of Beer on the Wall."
- bottles = 100
- while bottles > 1
- puts (bottles-1).to_s + ' bottles of beer on the wall, ' + (bottles-1).to_s + ' bottles of beer!'
- puts 'You take one down, pass it around, ' + (bottles-2).to_s + ' bottles of beer on the wall!'
- bottles = (bottles-1)
- end
- # Write a Deaf Grandma program. Whatever you say to grandma (whatever you type in), she should respond with HUH?! SPEAK UP, SONNY!, unless you shout it (type in all capitals). If you shout, she can hear you (or at least she thinks so) and yells back, NO, NOT SINCE 1938! To make your program really believable, have grandma shout a different year each time; maybe any year at random between 1930 and 1950. (This part is optional, and would be much easier if you read the section on Ruby's random number generator at the end of the methods chapter.) You can't stop talking to grandma until you shout BYE.
- # Hint: Don't forget about chomp! 'BYE'with an Enter is not the same as 'BYE' without one!
- # Hint 2: Try to think about what parts of your program should happen over and over again. All of those should be in your while loop.
- puts 'Uh oh! Looks like Grandma went deaf today.'
- puts 'Try saying something to get her attention:'
- speak = gets.chomp
- while speak != speak.upcase or speak != 'BYE'
- puts 'HUH!? SPEAK UP, SONNY! Hint: Try shouting or say BYE'
- speak = gets.chomp
- if speak == speak.upcase
- puts 'NO, NOT NOT SINCE ' + (1930+rand(21)).to_s + '!' + ' Hint: Try saying *BYE*'
- speak = gets.chomp
- end
- if speak == 'BYE'
- puts 'Grandma gives you the finger and storms away.'
- end
- end
- # Extend your Deaf Grandma program: What if grandma doesn't want you to leave? When you shout BYE, she could pretend not to hear you. Change your previous program so that you have to shout BYE three times in a row. Make sure to test your program: if you shout BYE three times, but not in a row, you should still be talking to grandma.
- puts 'Uh oh! Looks like Grandma went deaf today.'
- puts 'Try saying something to get her attention:'
- response = 'nope'
- bye = 0
- while bye < 3
- response = gets.chomp
- if response == (response.upcase and 'BYE')
- puts 'Hmmm... What about...'
- bye = (bye+1)
- end
- if response != response.upcase
- puts 'Huh?! I CAN\'T HEAR YOU!'
- end
- if (response == response.upcase and response != 'BYE')
- puts 'NO! NOT SINCE ' + (1930+rand(21)).to_s + '!'
- end
- end
- # Leap Years. Write a program which will ask for a starting year and an ending year, and then puts all of the leap years between them (and including them, if they are also leap years). Leap years are years divisible by four (like 1984 and 2004). However, years divisible by 100 are not leap years (such as 1800 and 1900) unless they are divisible by 400 (like 1600 and 2000, which were in fact leap years). (Yes, it's all pretty confusing, but not as confusing has having July in the middle of the winter, which is what would eventually happen.)
- puts 'This program will determine what leap years are between two specific dates that you assign.'
- puts ''
- puts 'What would you like your starting year to be?'
- start_year = gets.chomp.to_i
- puts ''
- puts 'What would you like your ending year to be?'
- end_year = gets.chomp.to_i
- puts "Leap years between #{start_year} and #{end_year}"
- if start_year > end_year
- puts 'Your closing date is before your start date. Please try again'
- return
- end
- leap_years = []
- start_year.upto(end_year) do |year|
- if (year % 4 == 0)
- leap_years << year unless (year % 100 == 0) and (year % 400 != 0)
- end
- end
- puts leap_years.join(', ')
- # Some notes:
- # - you can use the Integer#upto method to step through each year (see http://www.ruby-doc.org/core/ for API docs)
- # - remember to cast the user input into integers (and add error-handling, which I have left out in the code above)
- # - the % operator returns the remainder... E.g. 10 % 3 gives you 1.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement