Java language basic selection structure of switch statement and its interpretation Tooho King

Source: Internet
Author: User

Format of the switch statement

Inx = 10

switch (expression) {

Case value 1;

Break

Case value 2;

Break

...

Defualt:

Statement n-1;

Break

}

Note Btye Short char String can be used as an expression

Long No.

That is, the type that can be converted to int in the base type can be used as a case expression.

1  Public classtest1_datatypeconversion {2 3      Public Static voidMain (string[] args) {4String name = "Zhang San";5String gender = "Man";6             Switch(gender) {7                  CaseMen:8System.out.println ("Play games");9                  Break;Ten                  CaseMs.: OneSystem.out.println ("Make-up"); A                  Break; -                 default: -System.out.println ("You are a shemale"); the                  Break; -                -            } -               +               -     } +  A  at  -}

Attention

One break the last one can be omitted, others cannot be omitted

The second default can be placed in any position but it is recommended to put it on the last side

1  Public classHello_world {2 3      Public Static voidMain (string[] args) {4         5Scanner sr =NewScanner (system.in);6         7         intmonth =sr.nextint ();8         Switch(month) {9          Case1:Ten          Case2: One          Case3: ASystem.out.println ("one Quarter"); -          Break; -          Case4: the          Case5; -          Case6: -System.out.println ("Two Quarter")); -          Break; +              -          +          A         } at  -     } -  -}

Java language basic selection structure of switch statement and its interpretation Tooho King

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.