I've been using this throughout the semester as it's very helpful giving the user options to choose between various tasks as well as return back to the original menu. In this program I've chosen two options the user can choose from and if it wants to exit the program the user chooses option 3. I've left the options as well as the cases empty as this is only an example of the menu.
import java.io.IOException;
import java.util.*;
/**
*
* @author Erin Bell
* Example1: Implement a program with the options to do various tasks from the user.
*
*/
public class menu
{
/**
* This function displays the menu for the user to choose an option from
*/
public void Menu()
{
System.out.println("\nMenu: ");
System.out.println("______________________");
System.out.println("Option 1: Select 1 ");
System.out.println("Option 2: Select 2");
System.out.println("Option 3: Quit program");
}
public menu() throws IOException
{
//Accepts user input
//calls the menu method
//Initializes the run variable making the program loop until the user terminates the program
Boolean run=true;
while(run)
{
Menu();
switch (new Scanner(System.in).nextInt())
{
case 1:
System.out.println("Option 1 Selected");
System.out.println("Do something");
break;
case 2:
System.out.println("Option 2 selected");
System.out.println("Do something");
break;
case 3:
System.out.println("Program terminated");
//Terminates user input
run=false;
break;
//default case if one of the options selected were not allowed
default:
System.out.println("Not one of the options 1-3");
System.out.println("Program terminated.");
break;
}
}
}
/**
* Main method
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
//Calls the constructor
new menu();
}
}
Projects
Subscribe to:
Post Comments (Atom)
Recent Posts
Follow us on facebook
Popular Posts
-
This program uses semaphores ( data type that is used for controlling multiple processes to a common resource in a parallel programming)...
-
This is a c++ program implementing an event tracking system consisting of 5 separate programs executing simultaneously sending messages from...
-
This project was a software engineering assignment myself and a group of two other students had to document the process of the Monopoly Em...


No comments:
Post a Comment