Switch selection structure:
Why use switch to select a structure:
Switch selection structure can better solve the equivalence judgment problem.
Switch Select Four keywords for the structure:
Switch case Default break
Switch selection structure syntax:
Switch (key) { case value: break; Case Value: Break ; Case Value: Break ; default : Break ;} The value is matched according to key, if value is the same as key, then the code in the case block is taken, and then it is finished, If all case blocks do not match, they will go to default. If you want the case block to be executed, jump out of the selection structure and add a break at the end of the block.
Case:
/*Han Yu participate in the computer programming contest if you get the first prize, you will participate in the MIT 1-month summer camp. If you get second prize, you will be rewarded with a third prize on the HP laptop, which will reward the mobile HDD one otherwise, do not give any reward*/ Public classDemo01 { Public Static voidMain (string[] args) {Scanner input=NewScanner (System.inch); System. out. println ("Please enter the rank:");CharCount = Input.next (). CharAt (0);/** * Han YU participate in computer programming contest if you get the first prize, you will participate in the MIT 1-month summer camp if you get second place, you will be rewarded with an HP laptop * If you get a third place, you will be rewarded for moving the hard drive one otherwise, do not give any reward*/Switch(count) { Case 'a': System. out. println ("attend a 1-month summer camp organised by the Massachusetts Institute of Technology"); Break; Case 'two': System. out. println ("Reward hp Notebook PC"); Break; Case 'three': System. out. println ("Reward mobile HDD One"); Break; default: System. out. println ("No Rewards! "); Break; } }}
2. Note the question:
The constants after 1.case must be different
2. If you need to jump out after each case, do not forget to write a break after each.
3.default block order can be changed, but pay attention to its execution order. Typically, the default block is placed at the end, or you can omit
3. Multiple if selection structure and switch alignment
Same point:
Are all structures used to handle multi-branch conditions.
Different points:
Switch selection structure
Can only deal with the situation of the equivalence condition judgment
Multiple if selection structure
There is no restriction on switch selection structure, especially when a variable is in a continuous interval
4. Summarize the selection structure:
If selection structure
Basic if selection structure: situations where single or combined conditions are handled
IF-ELSE selection structure: Handling simple conditional branching situations
Multiple if selection structure: dealing with complex conditional branching situations
Nested IF selection structure: For more complex process Control
Switch selection structure
Multiple branches and condition judgement is the case of equivalence judgment
Public classDemo02 { Public Static voidMain (string[] args) {Scanner input=NewScanner (System.inch); System. out. println ("Welcome to your own management system"); System. out. println ("\t\t1. Login System"); System. out. println ("\t\t2. Exit"); System. out. println ("Please select:");if(Input.hasnextint ()) {intNumber=input.nextint ();Switch(number) { Case 1: System. out. println ("Welcome to login System!! "); Break; Case 2: System. out. println ("you quit the system!! "); Break;default: System. out. println ("No input options"); Break;}}Else{System. out. println ("Please enter the correct number:");}}}
Small white Java Learning path selection structure (ii)