switch case 相关介绍和没有break 和default 位置乱放分析

关于java中switch使用的一些说明
switch(表达式)
{
case 常量表达式1:语句1;
....
case 常量表达式2:语句2;
default:语句;
}
default就是如果没有符合的case就执行它,default并不是必须的.
case后的语句可以不用大括号.
switch语句的判断条件可以接受int,byte,char,short,不能接受其他类型.
如果使用long类型的话编译时会有错误产生,这点在使用上要注意,其他的数据类型都不行。
简单地说就是能够自动转换程int类型的数据类型才行
而case是指switch小括号中的变量会出现且你想要处理的值,它除了可以是个整数、字符之外,还可以是一些简单的算术表达式,不过算数表达式的结果要满足刚刚所说的四种数据类型。一旦case匹配,就会顺序执行后面的程序代码,而不管后面的case是否匹配,直到遇见break,利用这一特性可以让好几个case执行统一语句
Java代码   收藏代码
  1. 1.package flowcontrol;     
  2. 2.    
  3. 3.public class SwitchCase {     
  4. 4.    // first default     
  5. 5.    public static void testFirst(int i) {     
  6. 6.        switch (i) {     
  7. 7.        default:     
  8. 8.            System.out.println("default");// first default     
  9. 9.        case 1:     
  10. 10.            System.out.println("one");     
  11. 11.        case 2:     
  12. 12.            System.out.println("two");     
  13. 13.        case 3:     
  14. 14.            System.out.println("there");     
  15. 15.        }     
  16. 16.    }     
  17. 17.    
  18. 18.    // last default     
  19. 19.    public static void testLast(int i) {     
  20. 20.        switch (i) {     
  21. 21.        case 1:     
  22. 22.            System.out.println("one");     
  23. 23.        case 2:     
  24. 24.            System.out.println("two");     
  25. 25.        case 3:     
  26. 26.            System.out.println("there");     
  27. 27.        default:     
  28. 28.            System.out.println("default");// last default     
  29. 29.        }     
  30. 30.    }     
  31. 31.    
  32. 32.    // middle default     
  33. 33.    public static void testMiddle(int i) {     
  34. 34.        switch (i) {     
  35. 35.        case 1:     
  36. 36.            System.out.println("one");     
  37. 37.        case 2:     
  38. 38.            System.out.println("two");     
  39. 39.        default:     
  40. 40.            System.out.println("default");// middle default     
  41. 41.        case 3:     
  42. 42.            System.out.println("there");     
  43. 43.    
  44. 44.        }     
  45. 45.    }     
  46. 46.    
  47. 47.    public static void main(String[] args) {     
  48. 48.        // first default     
  49. 49.        testFirst(2);     
  50. 50.        System.out.println("------------------");     
  51. 51.        testFirst(9);     
  52. 52.    
  53. 53.        System.out.println("|||||||||||||||||||||||||||||||||||");     
  54. 54.    
  55. 55.        // last default     
  56. 56.        testLast(2);     
  57. 57.        System.out.println("----------------");     
  58. 58.        testLast(9);     
  59. 59.    
  60. 60.        System.out.println("|||||||||||||||||||||||||||||||||||");     
  61. 61.        // middle default     
  62. 62.        testMiddle(2);     
  63. 63.        System.out.println("----------------");     
  64. 64.        testMiddle(9);     
  65. 65.    
  66. 66.    }     
  67. 67.    
  68. 68.}    
输入结果:
Java代码   收藏代码
  1. 1.two     
  2. 2.there     
  3. 3.------------------     
  4. 4.default    
  5. 5.one     
  6. 6.two     
  7. 7.there     
  8. 8.|||||||||||||||||||||||||||||||||||     
  9. 9.two     
  10. 10.there     
  11. 11.default    
  12. 12.----------------     
  13. 13.default    
  14. 14.|||||||||||||||||||||||||||||||||||     
  15. 15.two     
  16. 16.default    
  17. 17.there     
  18. 18.----------------     
  19. 19.default    
  20. 20.there  

看了结果,可以这样理解:
(1)switch语句关键地方是进入点,有了进入点没有break的情况下会执行到底
(2)没有匹配值的时候default就是进入点,进入default以后会和普通进入点一样,如果没有break继续执行下面语句
(3)如果有break 则是从进入点到 break中间的语句执行


http://cjhbest999.iteye.com/blog/1137124

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值