Switch with char value in Java

Description

The following code shows how to switch with char value.

Example


// w w  w . j a v  a  2s .  com
import java.util.Scanner;

public class Main {
  static Scanner sc = new Scanner(System.in);

  public static void main(String[] args) {
    char p = 'a';

    String details = "";

    switch (p) {
    case 'E':
    case 'e':
      details += "\tE...\n";
    case 'D':
    case 'd':
      details += "\tD...\n";
    case 'C':
    case 'c':
      details += "\tC...\n";
    case 'B':
    case 'b':
      details += "\tB...\n";
    case 'A':
    case 'a':
      details += "\tA.\n";
      break;
    default:
      details = "That's";
      break;
    }
    System.out.println(details);
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Java Language »




Java Data Type, Operator
Java Statement
Java Class
Java Array
Java Exception Handling
Java Annotations
Java Generics
Java Data Structures