Locked lesson.
About this lesson
Allow the user to choose which type of flashcard game to play with a menu.
Exercise files
Download this lesson’s related exercise files.
Creating a Menu.docx60.5 KB Creating a Menu - Solution.docx
59.8 KB
Quick reference
Creating a Menu
Let's build a menu!
When to use
This technique will work whenver you need to create any sort of menu.
Instructions
First we need a menu to output a menu with options that people can choose:
def menu
menu_string = "1. Addition\n"
menu_string += "2. Subtraction\n"
menu_string += "3. Mulitplication\n"
menu_string += "4. Division\n"
menu_string += "5. End Game\n"
menu_string += "What Flashcard Would You Like? (1-4): "
return menu_string
end
Next we need a loop that will take action based on which of the menu items gets selected:
menu_choice = 0
while menu_choice < 6 do
puts menu
menu_choice = gets.to_i
if menu_choice == 1
add
elsif menu_choice == 2
subtract
elsif menu_choice == 3
multiply
elsif menu_choice == 4
divide
else
menu_choice == 5
exit
end
end
Hints & tips
- A while loop works great for creating a menu
- Put your menu options in a method
Lesson notes are only available for subscribers.