Java Reference
In-Depth Information
EXAMPLE 7-12(MENU DRIVEN PROGRAM)
The following is an example of a menu driven program. When the program executes, it
gives the user a list of choices to choose from. It converts length from feet and inches to
meters and centimeters and vice versa. The program contains three methods:
showChoices
,
inchesToCentimeters
, and
centimetersToInches
. The method
showChoices
informs the user how to use the program. The user has the choice to
run the program as long as the user wishes.
//Menu driven program.
import
java.util.*;
public class
Conversion
{
static
Scanner console =
new
Scanner(System.in);
static final double
CONVERSION = 2.54;
public static void
main(String[] args)
{
7
int
inches;
int
centimeters;
int
choice;
do
{
showChoices();
choice = console.nextIn();
System.out.println();
switch
(choice)
{
case
1:
System.out.print("Enter inches: ");
inches = console.nextInt();
System.out.println();
System.out.printf("%d inch(es) = %d centimeter(s)%n",
inches, inchesToCentimeters(inches));
break
;
case
2:
System.out.print("Enter centimeters: ");
centimeters = console.nextInt();
System.out.println();
System.out.printf("%d centimeter(s) = %d inch(es)%n",
centimeters, centToInches(centimeters));
break
;
case
99:
break
;
Search WWH ::
Custom Search