JavasSE-03 Java selection structure

Source: Internet
Author: User

Learning Essentials
    • If selection structure
    • Switch selection structure

If select Structure Single Branch if selection structure

Grammatical structure

Application situations

Question: If Kaylee's Java exam scores are more than 98 points, Jack Bauer can get a iphone8 as a reward.

The IF selection structure under complex conditions

Question: Xiao Qiang's Java score is greater than 98 points, and the music score is more than 80 points, the teacher rewarded him, or the Java score is equal to 100 points, the music score is more than 70 points, the teacher can reward him.

logical operators

Operator

An expression

Description

&&

Condition 1 && Conditions 2

Only two conditions are true at the same time, the result is true

||

Condition 1 | | Condition 2

As long as two conditions have one true, the result is true

!

! Conditions

If the condition is true, the result is false

When the condition is false, the result is true

Solution:

Note: short-circuit and priority issues for logical operators. For example "| |" The last condition is not calculated if the previous condition of the symbol is true.

Dual-Branch if selection structure

Grammatical structure

Flow chart

How to draw a flowchart

Rounded rectangles: The beginning and end of a program

Rectangle: Execute

Diamond: Judging

Trapezoid: Input and output

Arrows: Program Flow

On-Machine practice

Requirement Description: If the sports lottery in 5 million, I buy a car, funding project hope, to Europe travel; if not, I buy a sports lottery, continue to ecstatic.

Implementation: 1, draw a flowchart, 2, code implementation.

On-Machine practice

Requirement Description: Enter the 4-digit number from the keyboard. A four-digit average takes an integer, and the result equals the resulting random number as a lucky member.

Implementation: 1, draw a flowchart, 2, code implementation.

Hint: 1, use IF-ELSE Select structure, output winning or thank support information; 2, 0-9 random number generation method: int random= (int) (Math.random () *10);

Multi-branch if selection structure

Question: Student performance evaluation

Score >=80: Good

Score >=60: Medium

Score <60: Poor

Solution Solutions

    • Divide the score into several consecutive intervals to judge
    • Single If selection structure cannot be completed
    • Multiple if selection structures are cumbersome
    • Using multiple if selection structures

Analysis of multi-branch selection structure grammar

On-Machine practice

Requirements Description: I want to buy a car, buy what car depends on how much money I have in the bank

If my deposit is over 5 million, I'll buy a Ferrari.

Otherwise, if my deposit is more than 1 million, I will buy a BMW

Otherwise, if my deposit is over 500,000, I'll buy a Mercedes.

Otherwise, if my deposit is more than 100,000, I will buy the public

Otherwise, if my deposit is under 100,000, I buy a Czech

Requirements: Code implementation.

Note: Multi-branch selection structure condition order problem.

Nested IF selection structure

Question: The school holds the sports meeting, the hundred meters race runs into 10 seconds the student to be eligible to enter the final, according to the gender separately enters the male group and the woman group.

Solution:

To determine whether to enter the final

In the case of the final, determine whether to enter the men's group or enter the women's group

Use nested IF selection structure

Nested IF selection structure syntax analysis

On-Machine Exercises: Please write the code implementation of this problem

Switch selection structure

Problem: Kaylee in computer programming contest

If you get the first place, you will attend a 1-month summer camp organised by the Massachusetts Institute of Technology

If you get second place, you will be rewarded with an HP notebook computer

If you get third place, you will be rewarded with a mobile drive

Otherwise, do not give any reward

Solution:

Using multiple if selection structure implementations: drawbacks-complex structure, verbose

Using switch to select structure Solution: Condition--equivalence judgment

Switch selection structure Syntax analysis

Note: The use of break.

For example: 1,3,5 eat fast food, 2,4,6,7 eat a big meal, how to realize?

Practice on Computer: Realization of menu of student information Management system

The difference between a multiple if selection structure and a switch selection structure

Select structure

Same point

Different points

Switch selection structure

Are all structures used to handle multi-branch conditions.

Can only deal with the condition of the equivalence criterion, and the condition must be an integer variable, a character variable, or a string

Multiple if selection structure

There is no restriction on switch selection structure, especially when a variable is in a continuous interval

JavasSE-03 Java selection 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.