No. 4

Source: Internet
Author: User

After writing for one night, my knees and above are all paralyzed;

Next, select structure 2 (syntax ):

Swith (expression ){

Case constant 1:

// Code block 1;

Break;

Case constant 2:

// Code block 2;

Break;

.........

Default:

// Code block N;

Break;

}

Switch: equivalent judgment (only int and char can be judged). When multiple branches are required and the condition judgment is equivalent, the switch structure is simpler.

Of course, when using the switch structure, do not forget to add the break statement after each case.

Switch: indicates "Switch ". This switch is the value of the parentheses following the switch keyword. An integer variable (expression) or variable (expression) must be placed in the parentheses );

Case: It indicates "situation, situation". After case, it must be an integer or complex type constant, usually a fixed character, number, such as 8, 'A '. There can be multiple case blocks and the order can be changed, but the values of constants after each case must be different.

Default: indicates "default", that is, none of the above conditions is met. Followed by the colon. The order of defaule blocks and case blocks can be changed without affecting the execution result of the program. Generally, the default block is placed at the end or omitted.

Break: indicates "stop", that is, the current structure is exceeded.

If you want to enter a number for fear of entering characters, the system prompts you:

Input. hasnext

Example: int type is required,

Add an IF (input. hasnextint () = true ){

This line is accepted from the console

Then switch structure

} Else {

System. Out. println ("your input is incorrect. Please enter the correct number ");

}

 

No. 4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.