1 /*Switch Statement2 format:3 switch (expression)4 {5 Case takes value 1:6 execute the statement;7 Break ;8 Case takes value 2:9 execute the statement;Ten Break ; One ..... A Default: - execute the statement; - Break ; the } - - */ - classSwitchdemo + { - Public Static voidMain (string[] args) + { A intx = 3; at Switch(x)/*Features: 1 The selected type can only support four types: byte short int char - 2, the alternative answer does not specify the order, but the execution will start with the first case, each case is executed, and if there is a match, the execution is complete . - The switch is ended by a break in case, and if there is not a match, the default is executed. - - The switch statement ends in only two cases: 1 execution to break 2 execution to the end of the switch statement - */ in - { to Case4: +System.out.println ("a"); - Break; the Case2: *System.out.println ("B"); $ Break;Panax Notoginseng Case3: -System.out.println ("C"); the Break; + default: ASystem.out.println ("D"); the Break; + - } $ $ inta=4,b=2; - Charch = ' + '; - Switch(CH) the { - Case+:WuyiSystem.out.println (A +b); the Break; - Case‘-‘: WuSystem.out.println (A-b); - Break; About Case‘*‘: $System.out.println (A *b); - Break; - Case‘/‘: -System.out.println (A/b); A Break; + default: theSystem.out.println ("Nono") - $ the } the theSystem.out.println ("Hello world!"); the - } in the the}
Switch statement Practice
classswitchtest{ Public Static voidMain (string[] args) {/*int week = 2; Switch (week) {case 1:system.out.println (week+ "is Monday"); Break Case 2:system.out.println (week+ "is Tuesday"); Break Default:System.out.println (week+ "does not exist"); } System.out.println ("Hello world!"); */ intMonth =4; /*Switch (month) {Case 3:system.out.println (month + "Monthly is Spring"); Break Case 4:SYSTEM.OUT.PRINTLN (month + "Monthly is Spring"); Break Case 5:SYSTEM.OUT.PRINTLN (month + "Monthly is Spring"); Break DEFAULT:SYSTEM.OUT.PRINTLN (month + "months not present"); Break }*/ Switch(month) { Case3: Case4: Case5: System.out.println (Month+ "Month is Spring"); Break; Case6: Case7: Case8: System.out.println (Month+ "Month is Summer"); Break; Case9: Case10: Case11: System.out.println (Month+ "Month is Autumn"); Break; Case12: Case1: Case2: System.out.println (Month+ "Month is Winter"); Break; default: System.out.println (Month+ "Month does not exist"); Break; } } }
Java_ The switch statement of the basic syntax