Menu driven Java program

In this program we have used switch case statement to implement user friendly program. The java.util.Scanner class is used to read input from keyboard at runtime. If no choice is match then the deafault statement will get executed.

PROGRAM
import java.util.Scanner;

public class MenuDrivenProg {

 public static void main(String[] args) {
  
  int choice, num;
  Scanner n = new Scanner(System.in);
  System.out.println("Menu....");
  System.out.println("1. Find positive");
  System.out.println("2. Odd/Even");
  System.out.println("Enter the choice : ");
  choice = n.nextInt();
 
  switch(choice) {
  
   case 1:
    System.out.println("Enter number : ");
    num = n.nextInt();
    if(num > 0)
     System.out.println("Positive...");
    else
     System.out.println("Negative...");
    break;
    
   case 2:
    System.out.println("Enter number : ");
    num = n.nextInt();
    if(num%2 == 0)
     System.out.println("Even...");
    else
     System.out.println("Odd...");
    break;
    
   default:
    System.out.println("Wrong choice..");
  }
 }
}
OUTPUT 1
C:\>javac MenuDrivenProg.java
C:\>java MenuDrivenProg
Menu....
1. Find positive
2. Odd/Even
Enter the choice :
1
Enter number :
-20
Negative...
OUTPUT 2
C:\>javac MenuDrivenProg.java
C:\>java MenuDrivenProg
Menu....
1. Find positive
2. Odd/Even
Enter the choice :
2
Enter number :
63
Odd...

Comments

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Program to input age from user and throw user-defined exception if entered age is negative

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.