Dark Horse programmer--java Basic Grammar (ii) grammatical structure

Source: Internet
Author: User
Tags case statement

-----------android training ,Java training , Java Learning Technology blog, look forward to communicating with you! ------------ 

There are four syntax constructs in JavaSequential structure: As the name implies, the sentence is executed in order to determine the structure: If switch selection structure: Loop structure: For and do While 1. If StructureThe IF statement has three structures. (1) if (conditional expression) {EXECUTE statement;} (2) if (conditional expression) {EXECUTE statement;} else{execute statement;} (3) if (conditional expression) {EXECUTE statement;} else if (conditional expression) {EXECUTE statement;} ... else if (conditional expression) {EXECUTE statement;} else {EXECUTE statement;} Note: The ternary operator is the shorthand representation of the IF statement. Can simplify the code. But because it is an operator, you have to have a result. So the functional range is not as wide as the IF statement. Cases:
1 /*2 use the IF statement to determine if you want to enter the men's or the ladies .3 */4 classtestif5 {6      Public Static voidMain (String[]args)7     {    8          intX=5;9          if(x==1)TenSystem.out.println ("You are sir, please enter the men's restroom!") "); One          Else if(x==2) ASystem.out.println ("You are a lady, please enter the ladies '!") "); -          Else -System.out.println ("Sorry, your gender is not recognized!") "); theSystem.out.println ("\t\t\t\t\t ———— green shirt produced! ————"); -     }                            -}
1 /*2 Requirements: Customers are required to enter the month after the display of which season. 3 3 4 5 spring; 6 7 8 Summer; 9 10 11 autumn; 12 1 2 winter;4 */5 classJijie6 {7      Public Static voidMain (string[] args)8     {9         //design with an IF conditional statementTen         intx=11; One         if(x<1| | X>12) ASystem.out.println ("No this month, please re-enter!") "); -         Else if(x>=3&&x<=5) -System.out.println (x+ "Month is spring. Flowers in full bloom, more go out to walk, feel the charm of nature! "); the         Else if(x>=6&&x<=8) -System.out.println (x+ "Month is summer. Hot sun, please pay attention to the summer! "); -         Else if(x>=9&&x<=11) -System.out.println (x+ "Month is autumn.) Autumn, is a good time for tourism! "); +         Else -System.out.println (x+ "Month is winter. Please keep warm! "); +          ASystem.out.println ("\t\t\t\t\t\t ———— green shirt produced! ————"); at      -     } -}

2.switch structure

(1) Switch statement structure:

switch (expression) {case value 1: EXECUTE statement; break, case value 2: EXECUTE statement; break;......case value N: Execute statement; Break;default: execute statement; break;}

(2) switch statement features:

There are only four types in the A:switch statement: byte short int char

B:case and default write code without order, default can be placed in front of case. However, the execution order is still the first case, and the default statement is executed first.

C: There are two cases of ending a switch statement: one that encounters a break; The other is that the switch statement is executed (that is, the curly brace "}" in switch is encountered)

The D:case statement has a penetrating nature. That is, if the matching case statement does not break, then execution continues to execute the executed statement until it encounters a break, or the curly brace "}".

Cases:

1 /*2 Requirements: Customer input can display the corresponding week3 */4 classSwitch5 {6      Public Static voidMain (string[] args)7     {8         intX=1;9         Switch(x)Ten         { One              Case1: ASystem.out.println (x+ "Monday"); -              Case2: -System.out.println (x+ "Tuesday"); the              Case3: -System.out.println (x+ "Wednesday"); -                  Break; -              Case4: +System.out.println (x+ "Thursday"); -                  Break; +              Case5: ASystem.out.println (x+ "Friday"); at                  Break; -              Case6: -System.out.println (x+ "Saturday"); -                  Break; -              Case7: -System.out.println (x+ "Sunday"); in                  Break; -             default: toSYSTEM.OUT.PRINTLN ("Cannot query the number you entered, please re-enter"); +         } -  the          *     } $}

As the code above, the execution will continue to execute downward, the printed result will become,

The statement is not ended until a break is encountered. This is the penetrating nature of the case.

Let's do an exercise by using the switch structure to design the above input month display season.

1 /*2 Requirements: Customers are required to enter the month after the display of which season. 3 3 4 5 spring; 6 7 8 Summer; 9 10 11 autumn; 12 1 2 winter;4 */5 classJijie6 {7      Public Static voidMain (string[] args)8     {9         //using the switch statement to designTen         intA=5; One         Switch(a) A         { -              Case3: -              Case4: the              Case5: -System.out.println (A + "month is spring.) Flowers in full bloom, more go out to walk, feel the charm of nature! "); -                  Break; -              Case6: +              Case7: -              Case8: +System.out.println (A + "month is summer.) Hot sun, please pay attention to the summer! "); A                  Break; at              Case9: -              Case10: -              Case11: -System.out.println (A + "month is autumn.) Autumn, is a good time for tourism! "); -                  Break; -              Case12: in              Case1: -              Case2: toSystem.out.println (A + "month is winter.) Please keep warm! "); +                  Break; -             default: theSystem.out.println ("No this month, please re-enter!") "); *  $         }Panax Notoginseng          -System.out.println ("\t\t\t\t\t\t ———— green shirt produced! ————"); the      +     } A}

Dark Horse programmer--java Basic Grammar (ii) grammatical structure

Related Article

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.