zoukankan      html  css  js  c++  java
  • java基础之switch

    switch 语句由一个控制表达式和多个case标签组成。

    switch 控制表达式支持的类型有byte、short、char、int、enum(Java 5)、String(Java 7)。

    switch-case语句完全可以与if-else语句互转,但通常来说,switch-case语句执行效率要高。

    default在当前switch找不到匹配的case时执行。default并不是必须的。

    一旦case匹配,就会顺序执行后面的程序代码,而不管后面的case是否匹配,直到遇见break。

    switch语法格式

    switch (表达式) {
    case 条件1:
        语句1;
        break;
    case 条件2:
        语句2;
        break;
    ...
    default:
        语句;
    }

    分支语句先对表达式进行求值,然后依次匹配条件1,条件2... ...当表达式的值于条件相匹配时,执行对应的语句,如果没有对应对条件,则执行default语句。

    byte:

     1 byte b1=12;
     2         switch(b1){
     3         case 1:
     4             System.out.println("wrong");
     5             break;
     6         
     7         case 2:
     8             System.out.println("wrong2");
     9             break;
    10         
    11         case 12:
    12             System.out.println("right");
    13             break;
    14             
    15         default:
    16             System.out.println("all wrong");
    17         }

     int:

     1 int num1=12;
     2         switch(num1){
     3         case 1:
     4             System.out.println("wrong");
     5             break;
     6         
     7         case 2:
     8             System.out.println("wrong2");
     9             break;
    10         
    11         case 12:
    12             System.out.println("right");
    13             break;
    14             
    15         default:
    16             System.out.println("all wrong");
    17         }

    char:

     1 char c1='a';
     2         switch (c1) {
     3         case 'a':
     4             System.out.println("a is right");
     5             break;
     6             
     7         case 'b':
     8             System.out.println("b is right");
     9             break;
    10             
    11         case 'c':
    12             System.out.println("c is right");
    13             break;
    14         default:
    15             break;
    16         }

    string:

     1 String str1="123";
     2         switch (str1) {
     3         case "111":
     4             System.out.println("111 is right");
     5             break;
     6 
     7         case "112":
     8             System.out.println("112 is right");
     9             break;
    10             
    11         case "123":
    12             System.out.println("123 is right");
    13             break;
    14         default:
    15             System.out.println(" no right");
    16             break;
    17         }

     Enum:

     1 static enum E{
     2         A,B,C,D
     3     }
     4     public static void main(String[] args) {
     5         E e=E.C;
     6         
     7         switch (e) {
     8         case A:
     9             System.out.println("A is right");
    10             break;
    11 
    12         case B:
    13             System.out.println("B is right");
    14             break;
    15 
    16         case C:
    17             System.out.println("C is right");
    18             break;
    19             
    20         case D:
    21             System.out.println("D is right");
    22             break;                
    23         default:
    24             System.out.println("no right");
    25             break;
    26         }
  • 相关阅读:
    ASP.Net设计时需要考虑的性能优化问题 转载自http://blog.sina.com.cn/s/blog_3d7bed650100055p.html
    Jqeruy dropdownlist 联动
    Sound Recording in Windows Phone 7
    Windows Phone Performance 系列网址集合
    【xml]: Read XML with Namespace resolution using XLinq.XElement
    编程基础字符篇(2)
    每日总结一:
    每日总结5:
    Control usage: (1) Windows Phone 7: Popup control
    编程基础字符篇(3)
  • 原文地址:https://www.cnblogs.com/jiayouxiage/p/6359642.html
Copyright © 2011-2022 走看看