Java Chapter III

Source: Internet
Author: User

   The selection structure (a) uses the basic if selection structure to master the logical operators, master the multiple if selection structure, and master the If selection structure, for example: If Zhang Hao's jav               A test score of more than 98 points, Zhang Hao will be able to obtain a MP4 as a reward using the if selection structure can be resolved.                                        The If selection structure is based on the conditional judgment and then the processing syntax: basic if selection structure if (condition) {//code block//result must be Boolean type      }//There is only one statement, it is recommended not to omit the combination problem: if (Zhang Hao Java Test score >90) {Get a MP4 as a reward;} int score = Input.nextint (); Get Zhang Hao Java score from the console if (score>90) {//judgment is greater than 90 System.out.println ("Teacher said: Yes, reward a MP4!") ")//} using the If selection structure under complex conditions Zhang Hao Java score is more than 98 points, and the music score is more than 80 points, the teacher rewarded him, or the Java score is equal to 100 music score more than 70 points, the teacher You can also reward him, expression: if (score1>98 && score2>80) | |         (score1==100 && score2> 70) Condition 1 && condition 2 operator; and, expressing meaning, two conditions are true at the same time, the result is true. Condition 1 | |            Condition 2 operator; or, to express the meaning, only two conditions have one true and the result is true.  " ! "The meaning operator of the inverse, the expression of meaning, the condition is true when the result is false." If the condition is false, the result is true use IF-ELSE selection structure If Zhang Hao Java scores more than 98 points, the teacher will reward MP4.                               Otherwise fine 200 the use of two basic if selection structure can also achieve the effect. Use If-else to select the structure implementation//if if (score>98) {System.out.println ("teacher says: Yes, reward MP4!               "); }//If if (score<=98) {System.out.println ("teacher says: Fine 200!              ");                  Use If-else to select the structure if (condition) {//code block 1}else {//code block 2} Binding problem: If (Zhang Hao Java score >98) {rewards a MP4;}else {punishes $200;} generates a random number (0~9)    The law is as follows: int random= (int) (Math.random () *10);  Use multiple if selection structure int score=70;                    Exam results if (score>=80) {//greater than or equal to 80 points System.out.println ("excellent");                      }else if (score>=60) {//greater than or equal to 60 points, less than 80 points System.out.println ("good");           }else {Less than 60 points System.out.println ("poor"); Why use nested IF selection structure: determine if you canEnough to get into the finals. In the case of the final, determine whether to enter the men's group, or into the women's group using nested if selection structure to run into 10 seconds if (condition 1) if (condition 2) {/      /gender is male,//code block 1}else {code block 2}//above is called: Inner selection structure} else {code block 3 } if (score<=10) {if (Gender.equals ("male")) {System.out.println ("Enter the men's team final!       "); }else if (gender.equals ("female")) {System.out.println ("Enter the women's team finals!       "); }}else {Sytem.out.println ("Obsolete!          ");          } If structure writing specification.                The IF structure of the content should have a certain indentation to match the outer if structure to a pair if and else should be left aligned in order to make the if structure clearer and avoid execution errors, each if or       Else contains code blocks enclosed in curly braces. Summary: The IF selection structure in Java, including the following form of the basic if selection structure: can handle single or combined conditions IF-ELSE selection structure: Can handle simple conditional branching situation multiple if selection                         Optional structure: Can handle the conditions of the segmented branch case nested IF selection structure: can handle complex conditional branching situation

Java Chapter III

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.