Saturday, June 22, 2019

Can you write the code for switch statements using enum in Java SE 8 ?

Problem statement:
Write a program for switch statements using enum in Java.
  1. enum APP {
  2.     Google, Yahoo, Apple, HP
  3. }
  4. public class SwitchStatement {
  5.     public static void main(String[] args) {
  6.         APP c = APP.Google;
  7.         switch (c) {
  8.             case Apple:
  9.                 System.out.println("You choose Apple.");
  10.                 break;
  11.             case Google:
  12.                 System.out.println("You choose Google.");
  13.                 break;
  14.             case Yahoo:
  15.                 System.out.println("You choose Yahoo.");
  16.                 break;
  17.             case HP:
  18.                 System.out.println("You choose HP.");
  19.                 break;
  20.             default:
  21.                 System.out.println("default");
  22.                 break;
  23.         }
  24.     }
  25. }
Output:
You choose Google.

No comments:

Post a Comment

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...