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

时间:2025-03-22 08:54:34
  • 1.package flowcontrol;     
  • 2.    
  • 3.public class SwitchCase {     
  • 4.    // first default     
  • 5.    public static void testFirst(int i) {     
  • 6.        switch (i) {     
  • 7.        default:     
  • 8.            ("default");// first default     
  • 9.        case 1:     
  • 10.            ("one");     
  • 11.        case 2:     
  • 12.            ("two");     
  • 13.        case 3:     
  • 14.            ("there");     
  • 15.        }     
  • 16.    }     
  • 17.    
  • 18.    // last default     
  • 19.    public static void testLast(int i) {     
  • 20.        switch (i) {     
  • 21.        case 1:     
  • 22.            ("one");     
  • 23.        case 2:     
  • 24.            ("two");     
  • 25.        case 3:     
  • 26.            ("there");     
  • 27.        default:     
  • 28.            ("default");// last default     
  • 29.        }     
  • 30.    }     
  • 31.    
  • 32.    // middle default     
  • 33.    public static void testMiddle(int i) {     
  • 34.        switch (i) {     
  • 35.        case 1:     
  • 36.            ("one");     
  • 37.        case 2:     
  • 38.            ("two");     
  • 39.        default:     
  • 40.            ("default");// middle default     
  • 41.        case 3:     
  • 42.            ("there");     
  • 43.    
  • 44.        }     
  • 45.    }     
  • 46.    
  • 47.    public static void main(String[] args) {     
  • 48.        // first default     
  • 49.        testFirst(2);     
  • 50.        ("------------------");     
  • 51.        testFirst(9);     
  • 52.    
  • 53.        ("|||||||||||||||||||||||||||||||||||");     
  • 54.    
  • 55.        // last default     
  • 56.        testLast(2);     
  • 57.        ("----------------");     
  • 58.        testLast(9);     
  • 59.    
  • 60.        ("|||||||||||||||||||||||||||||||||||");     
  • 61.        // middle default     
  • 62.        testMiddle(2);     
  • 63.        ("----------------");     
  • 64.        testMiddle(9);     
  • 65.    
  • 66.    }     
  • 67.    
  • 68.}