Java (3) if structure

Source: Internet
Author: User

first, basic if structure

1. Flowchart

1) Input/output

2) Judging and branching

3) Process Line

  1.1 Simple if condition to judge

if ( expression ) {

the // expression is true to execute the code in {}

}

Example 1 : If Zhang San's java score is greater than 98, then the teacher rewards him with a MP4.   

1  Public classPractice1 {2      Public Static voidMain (string[] args) {3                 //enter Zhang San's score from the console4Scanner input =NewScanner (system.in);5System.out.print ("Please enter the Java score for Zhang San:");6         intScore =input.nextint ();7         if(Score >98){8SYSTEM.OUT.PRINTLN ("Teacher said: Yes, rewarding a mp4! ");9         }Ten     } One}
If Structure

Run the results,

1) Realization of the conditions of operation results

2) operating results without conditions

  1.2 Simple if condition to judge

if ( expression ) {

// expression is true , execute here

}else{

// expression is false , this line here

}

Example 2   : If Zhang San's java score is greater than 98, then the teacher rewards him with a MP4, otherwise the teacher punishes him to copy the test paper. 

1  Public classPractice2 {2      Public Static voidMain (string[] args) {3Scanner input =NewScanner (system.in);4System.out.print ("Please enter the Java score for Zhang San:");5         intScore =input.nextint ();6         if(Score >98){7SYSTEM.OUT.PRINTLN ("Teacher said: Yes, rewarding a mp4! ");8}Else{9System.out.println ("The teacher said:" A copy of the paper));Ten         } One     } A}
If-else Structure

Run the results,

1) Realization of the conditions of operation results

2) Implementation of the conditions are not set up the results of the operation

Description: If the If or else is followed by, and there is only one line of code, {} can be omitted, but it is not recommended to omit

second, logical operators

A. && Logic and (press shift+7)--and (and, at the same time)

Multiple conditions, 1 are false, if () all false, multiple conditions are true at the same time, if () is true

B. | | (Press shift+ to enter the above |) --or (or) multiple conditions, as long as 1 are true, if () all are true

C. ! (in English state)--Take the inverse example: Not equal to! =

1) Determine if the strings are equal

A , case-sensitive: password

String Variables . Equals ( the value of a string or a string variable )

B , Case insensitive: Verification Code

String Variables . Equalsignorecase ( value of string or string variable )

2) How to break down numbers on single, 10, Hundred, thousand? "P47"

Int digit = number% 10//decomposition get single digit

Int 10 bit = number/10 10//decomposition get 10 digits

Int hundred = number/100% 10//decomposition to get the number of hundred

INT thousand = number/1000% 10//Decomposition get thousand

third, multiple if structure

if ( condition 1) {

// code block

}else if ( condition 2) {

// code block

}else if ( condition N) {

// code block

} else{

// code block

}

Example 3: Xiao Ming's classmates took part in the course of Java study, her parents admit Connaught:

1) If the test result equals 100 points, the father buys a car for her

2) If the test scores >=90, the mother buys her a laptop computer

3) If the test scores >=60, mother buys her a cell phone

4) If the test results <60 points, deduct one weeks of living expenses

1  Public classPractice3 {2      Public Static voidMain (string[] args) {3Scanner input =NewScanner (system.in);4System.out.println ("Please enter score:");5         intScore=input.nextint ();6         if(score== 100){7System.out.println ("Father buys her a car");8}Else if(score>= 90){9System.out.println ("Mother buys a laptop for her");Ten}Else if(score>=60){ OneSystem.out.println ("mother buys her a cell phone"); A}Else{ -System.out.println ("One weeks of living expenses deducted"); -         } the     } -}
multiple if selection structure

Run the results (sequentially):

  

  

  

  

Description: An else if can have multiple, sequential requirements on the number range

Enter String--if in if (variable name. Equals ("string")) {}

Iv. Nested IF Structures

if ( condition 1) {

                   // code block

I f ( condition 1-1) {

}

Description: The if structure can be a if , If...else , multiple if and other structures

} else{

can be for if , If...else , multiple if and other structures

}

Example 4: The School holds a sports meeting, the students in the hundred-meter race in 10 seconds are eligible to enter the final, according to the gender division of the men's and women's groups.

1  Public classPractice4 {2      Public Static voidMain (string[] args) {3Scanner input =NewScanner (system.in);4System.out.print ("Please enter the match score (s):");5         DoubleScore =input.nextdouble ();6System.out.print ("Please enter gender");7String gender =Input.next ();8         if(score<=10){9             if(Gender.equals ("Male")){TenSystem.out.println ("Enter the men's team finals"); One}Else if(Gender.equals ("female")){ ASystem.out.println ("Enter the women's team finals"); -             } -}Else{ theSystem.out.println ("obsolete"); -         } -     } -}
Nested IF Structures

Run the results,

1.2.

3.4.

5.6.

Description: 1. The condition of the inner if is only judged if the condition of the outer if selection structure is satisfied

2.else is always paired with the missing else if

Note:The writing specification for the IF structure:

    --to make the if structure clearer, enclose the block of code that each if or else contains in curly braces.

--matched pairs of if and else should be left justified

-The IF structure of the inner layer must be indented relative to the outer if structure

Java (3) if structure

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.