Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

A simple Java program demonstrating how to use enum in switch statements

Program to demonstrate how to use enum in switch statements in Java

package com.hubberspot.examples;

import java.util.Scanner;

enum IndianPremierLeague {

 DECCAN_CHARGERS,
 KOLKATA_KNIGHT_RIDERS,
 MUMBAI_INDIANS,
 CHENNAI_SUPER_KINGS,
 RAJASTHAN_ROYALS,
 ROYAL_CHALLENGERS_BANGALORE,
 KINGS_XI_PUNJAB,
 PUNE_WARRIORS,
 DELHI_DAREDEVILS

}

public class SwitchEnumTest {


   public static void main(String[] args) {

 IndianPremierLeague team = IndianPremierLeague.MUMBAI_INDIANS;

 System.out.print("So you like: ");

 switch (team) {

           case DECCAN_CHARGERS:
  System.out.println("Deccan Chargers"); 
  break;

           case KOLKATA_KNIGHT_RIDERS:
  System.out.println("Kolkata Night Riders"); 
  break;

    case MUMBAI_INDIANS:
  System.out.println("Mumbai Indians"); 
  break;
   
           case CHENNAI_SUPER_KINGS:
  System.out.println("Chennai Super Kings"); 
  break;

    case RAJASTHAN_ROYALS:
  System.out.println("Rajasthan Royals"); 
  break;

    case ROYAL_CHALLENGERS_BANGALORE:
  System.out.println("Royal Challengers Bangalore"); 
  break;

    case KINGS_XI_PUNJAB:
    System.out.println("Kings XI Punjab"); 
  break;

    case PUNE_WARRIORS:
  System.out.println("Pune Warriors India"); 
  break;

    case DELHI_DAREDEVILS:
  System.out.println("Delhi Daredevils"); 
  break;                 

    default:
    System.out.println("Invalid IPL team choosen");
      }
   }
}






Output of the program :

So you like: Mumbai Indians
 
© 2021 Learn Java by Examples Template by Hubberspot