Java Tutorial/Statement Control/Switch Statement — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 05:18, 1 июня 2010

Execute the same statements for several different case labels

public class MainClass {
  
  public static void main(String[] args) {
    char yesNo = "N";
    switch(yesNo) {
      case "n": case "N":
           System.out.println("No selected");
           break;
      case "y": case "Y":
           System.out.println("Yes selected");
           break;
    }
  }
}



No selected


Free Flowing Switch Statement Example

public class Main {
  public static void main(String[] args) {
    int i = 0;
    switch (i) {
    case 0:
      System.out.println("i is 0");
    case 1:
      System.out.println("i is 1");
    case 2:
      System.out.println("i is 2");
    default:
      System.out.println("Free flowing switch example!");
    }
  }
}
/*
i is 0
i is 1
i is 2
Free flowing switch example!
*/





Nested Switch Statements Example

public class Main {
  public static void main(String[] args) {
    int i = 0;
    switch (i) {
    case 0:
      int j = 1;
      switch (j) {
      case 0:
        System.out.println("i is 0, j is 0");
        break;
      case 1:
        System.out.println("i is 0, j is 1");
        break;
      default:
        System.out.println("nested default case!!");
      }
      break;
    default:
      System.out.println("No matching case found!!");
    }
  }
}
//i is 0, j is 1





Switch statement with enum

public class MainClass {
  enum Choice { Choice1, Choice2, Choice3 }
  public static void main(String[] args) {
    Choice ch = Choice.Choice1;
    switch(ch) {
      case Choice1:
        System.out.println("Choice1 selected");
        break;
     case Choice2:
       System.out.println("Choice2 selected");
       break;
     case Choice3:
       System.out.println("Choice3 selected");
       break;
    }
  }
}



Choice1 selected


The switch Statement

  1. An alternative to a series of else if is the switch statement.
  2. The switch statement allows you to choose a block of statements to run from a selection of code, based on the return value of an expression.
  3. The expression used in the switch statement must return an int or an enumerated value.

The syntax of the switch statement is as follows.



switch (expression) {
case value_1 :
     statement (s);
     break;
case value_2 :
     statement (s);
     break;
  .
  .
  .
case value_n :
     statement (s);
     break;
default:
     statement (s);
}





The switch Statement: a demo

public class MainClass {
  public static void main(String[] args) {
    int choice = 2;
    switch (choice) {
    case 1:
      System.out.println("Choice 1 selected");
      break;
    case 2:
      System.out.println("Choice 2 selected");
      break;
    case 3:
      System.out.println("Choice 3 selected");
      break;
    default:
      System.out.println("Default");
      break;
    }
  }
}



Choice 2 selected