CS200 Computer Science I
Kevin Sahr, PhD
Lecture 14
Menu Application
1
Menus
✴common application approach: allow the
user to choose what to do via a menu
2
2
Pseudocode
✴an algorithm for a Menu:
✴
✴
✴
✴
do it again = true
while do it again
input menu choice
process menu choice
3
3
Loop Condition
✴can often simplify the logic of a loop by using a
boolean variable for the condition
boolean again;
again = true;
while (again)
{
✴
✴
✴
✴
set again to false if QUIT chosen
✴
} // while
✴
4
4
Pseudocode
✴an algorithm for a Menu:
✴
✴
✴
✴
do it again = true
while do it again
input menu choice
process menu choice
5
5
Processing Pseudocode
✴algorithm Process Menu Choice:
✴
✴
✴
✴
✴
✴
✴
✴
✴
if first choice chosen
do first choice action
else if second choice chosen
do second choice action
... etc. ...
else if quit chosen
do it again = false
else
output “invalid choice”
6
6
Menu Example
✴EX: have the user input a real number and
then use a menu to let the user choose to
square the number, take its inverse, or quit
7
7
Pseudocode
✴pseudocode for our solution:
✴
✴
✴
✴
✴
✴
input x
build menu prompt string
do it again = true
while do it again
input menu choice
process menu choice
8
8
Input the Number
✴
✴
double x;
x = Input.readDouble("Enter the number: ");
✴could echo the input value now, but slicker to
include the value in our menu prompt
9
9
Pseudocode
✴pseudocode for our solution:
✴
✴
✴
✴
✴
✴
input x
build menu prompt string
do it again = true
while do it again
input menu choice
process menu choice
10
10
Menu Prompt
✴build our menu prompt string
String menu;
menu = "Choose what to do with " +
x + ":\n";
menu = menu + "a. Square it.\n";
menu = menu + "b. Take its inverse.\n";
menu = menu + "c. QUIT.";
✴
✴
✴
✴
✴
✴
11
11
Pseudocode
✴pseudocode for our solution:
✴
✴
✴
✴
✴
✴
input x
build menu prompt string
do it again = true
while do it again
input menu choice
process menu choice
12
12
Input Menu Choice
✴use readChar (or readInt as appropriate) to let
the user choose from the menu
✴use the menu prompt String we built earlier
✴
✴
choice = Input.readChar(menu);
Output.showValue("You chose: ", choice);
• displays the menu prompt
string
• returns the user’s choice
• we will validate the choice
in the next step
13
13
Pseudocode
✴pseudocode for our solution:
✴
✴
✴
✴
✴
✴
input x
build menu prompt string
do it again = true
while do it again
input menu choice
process menu choice
14
14
Choice Pseudocode
✴processing a menu choice pseudocode:
✴
✴
✴
✴
✴
✴
✴
✴
if first choice chosen
output x squared
else if second choice chosen
output inverse of x
else if quit chosen
do it again = false
else
output “invalid choice”
15
15
Source Code
✴see Menu.java for complete Java
application
✴spend some time looking at and
experimenting with Menu.java
16
16
Module 14 Vocabulary
no new vocabulary
17
17
© Copyright 2026 Paperzz