The fourth day of Java basic learning (control flow statement)

1. Sequential sentences

Definition: Executing all code from top to bottom in code order is a sequential statement

Second, if judgment statement

1. Format of if judgment statement
① Format 1: It is suitable for use in one situation.

if(判断的条件){
    符合条件执行的代码;      
}

② Format 2: It is suitable for use in two situations.

if(判断条件){
    符合条件执行的代码;
}else{
    不符合条件执行的代码;
}

The if-else is very similar to the ternary operator:
the ternary operator has a simpler structure, but the ternary operator must return a result if it meets the conditions, and cannot execute the statement.
③ Format 3: It is suitable for use in a variety of situations.

if(判断条件1){
        符合条件1执行的代码;
    }else if(判断条件2){
        符合条件2执行的代码;
    }else if(判断条件3){
        符合条件3执行的代码;
    }......else{
        都不符合上述的条件执行的代码;
    }

2. Details to be paid attention to in the if statement
① If only one statement needs to be executed after the condition is met, the curly brackets can be omitted; but it is not recommended to omit because the structure is not clear
② The semicolon cannot be added after the judgment condition of the if statement, otherwise it will affect the effect of execution

class Demo4.1{
    public static void main(String[] args){ 
        //需求1:工作经验要两年或者两年以上
        int workAge = 2;            
        if(workAge>=2){
            System.out.println("电话通知你面试");
        }           
        if(workAge>=2){
            System.out.println("电话通知你面试");
        }else{
            System.out.println("电话通知不要再投简历了,不收你!!");
        }   

        //需求2:根据一个变量所记录的数字输出对应的星期。0代表星期天、1代表星期一……
        int num = 31;       
        if(num==0){
            System.out.println("星期天");
        }else if(num==1){
            System.out.println("星期一");
        }else if(num==2){
            System.out.println("星期二");
        }else if(num==3){
            System.out.println("星期三");
        }else if(num==4){
            System.out.println("星期四");
        }else if(num==5){
            System.out.println("星期五");
        }else if(num==6){
            System.out.println("星期六");
        }else{
            System.out.println("没有对应的星期");
        }
    }
}

3. Requirements: Input a score on the keyboard, and output the corresponding grade according to the score. For
example : 100~90[A grade], 89~80[B grade]...
Steps for entering data for the rest [E grade]: create a scanner object> Call the scanner object's nextInt method to scan the data > import package

import java.util.*;
class Demo4.2{
    public static void main(String[] args){ 
        //创建一个扫描器
        Scanner scanner = new Scanner(System.in);
        //调用扫描器扫描键盘录入的数据        
        System.out.println("请输入一个分数:");
        int score = scanner.nextInt(); //定义了一个num变量接收扫描到内容
        System.out.println("录入的数据是:"+score);        
        if(score>=90&&score<=100){
            System.out.println("A等级");
        }else if(score>=80&&score<=89){
            System.out.println("B等级");
        }else if(score>=70&&score<=79){         
            System.out.println("C等级");
        }else if(score>=60&&score<=69){         
            System.out.println("D等级");
        }else if(score>=0&&score<=59){          
            System.out.println("E等级");
        }else{
            System.out.println("补考..");
        }
    }
}

Three, switch selection judgment statement

1. Format of the switch statement

switch(你的选择){   
    case1
        符合值1执行的代码;
        break;
    case2
        符合值2执行的代码;
        break;
    case3
        符合值3执行的代码;
        break;
    case4
        符合值4执行的代码;
        break;
    ......
    default: 
        你的选择都符合上述的选项时执行的代码;
        break;
}

2. Matters needing attention in the
switch statement ① The variables used in the switch statement can only be of byte, char, short, int, and String data types. The String data type is supported since jdk7.0.
② The data following the case must be a constant.
③ Stop condition: Once the switch statement matches one of the case statements, the statement code in the corresponding case will be executed. After the execution, if the break keyword or the braces ending the switch statement are not encountered, then the switch statement No further judgment is made, and all the code is executed from top to bottom in the order of the code; until it encounters a break or the braces that end the siwitch statement.
④ In the switch statement, regardless of the order of the code, the case statement will always be judged first, and then the default statement will be executed if there is no match.
⑤ if-else if-else The if statement is very similar to the
switch statement: the structure of the switch statement is clear, but if the condition to be judged is an interval range, it is very troublesome to use the switch operation.

3. Requirements: Accept the keyboard to input a month, and output the corresponding season according to the corresponding month.
3 4 5 [spring]
6 7 8 [summer]
9 10 11 [fall]
1 2 12 [winter]
requires a switch statement implementation.

import java.util.*;
class Demo4.3{
    public static void main(String[] args){
        System.out.println("请输入一个月份:");
        //创建一个扫描器
        Scanner scanner = new Scanner(System.in);
        //调用扫描器的nextInt方法
        int month = scanner.nextInt();
        switch(month){
            case 3:
            case 4:
            case 5:
                System.out.println("春天");
                break;
            case 6:
            case 7:
            case 8:
                System.out.println("夏天");
                break;
            case 9:
            case 10:
            case 11:
                System.out.println("秋天");
                break;
            case 12:
            case 1:
            case 2:
                System.out.println("冬天");
                break;
            default:
                System.out.println("没有对应的季节");
                break;
        }
    }
}

Fourth, while loop statement

1. The format of the while loop statement

while(循环的条件){
    循环语句;
}

2. Matters needing attention in the
while loop statement ① The while loop statement generally controls the number of its loops through a variable, of which Ctrl+C can force the exit from the infinite loop.
② If there is only one statement in the loop body code of the while loop statement, the curly brackets can be omitted, but it is not recommended to omit
③ The judgment condition of the while loop statement cannot be followed by a semicolon, otherwise it will affect the effect of execution.

3. Demand: Calculate the sum of 1+2+3+….+ 100.

class Demo4.4{
    public static void main(String[] args){
        int num = 1;
        int sum  = 0;//定义一个变量用于保存每次相加的结果
        while(num<=100){
            sum = sum+num;
            num++;
        }
        System.out.println("sum = "+ sum);
    }
}

4. Requirement: Realize the game of guessing numbers. If you don't guess correctly, you can continue to input the number you guessed. If you guess correctly, stop the program. You can only guess three times at most, and remind the user if there is one last chance left.
Steps to create a random number: create a random number object > call the nextInt method of the random number object > import package

import java.util.*;
class Demo4.5{
    public static void main(String[] args){
        //创建一个随机数对象
        Random random = new Random();
        //调用随机数对象的nextInt方法产生一个随机数
        int randomNum = random.nextInt(10)+1; //要求随机数是:1~10
        //创建一个扫描器对象
        Scanner scanner = new Scanner(System.in);   
        boolean flag=true;  
        while(flag){
            System.out.println("请输入你要猜的数字:");
            //调用扫描器的nextInt方法扫描一个数字
            int guessNum = scanner.nextInt();
            if (guessNum>randomNum){
                System.out.println("猜大了..");
            }else if(guessNum<randomNum){
                System.out.println("猜小了..");    
            }else{
                System.out.println("恭喜你,猜对了`..");   
                break;
            }
        }       
    }
}

Five, do-while loop statement

1. Format

do{
    循环语句;
    }while(判断条件);

2. The difference between the while loop statement and the do-while loop statement The
while loop statement is to judge first and then execute the loop statement; the do-while loop statement is to execute first and then judge; it will be executed at least once regardless of whether the conditions are met.

class Demo4.6{
    public static void main(String[] args){
        //在java中,java编译器是不允许写废话。    
        int count=0;    
        do
        {
            System.out.println("hello world");
            count++;
        }while(count<5);
    }
}

3. Requirements: Use do-while to calculate the sum of even numbers between 1-100.

class Demo4.7{
    public static void main(String[] args){
        int num = 1;
        int sum = 0;//定义一个变量用于保存每次相加的总和
        do{
            if(num%2==0){
                sum += num;
            }
            num++;
        }while(num<101);
        System.out.println("sum = "+ sum);
    }
}

Six, for loop statement

1. The format of the for loop statement

for(初始化语句;判断语句;循环后的语句){
        循环语句;
    }

2. Matters needing attention in the for loop statement:
for(;;)This writing method is an infinite loop statement, which is equivalent to while(true)
② the initialization statement of the for loop statement will only be executed once in the first loop
③ When the loop body statement of the for loop statement has only one sentence , you can omit the curly brackets, but it is not recommended to omit

3. Requirements: Print five sentences of hello world on the control

class Demo4.8{
    public static void main(String[] args){         
        for(int count = 0 ; count<5;  count++){         
            System.out.println("hello world");
        }   
    }
}

4. Requirements: Print a rectangle with five rows and five columns on the console

    *****
    *****
    *****
    *****
    *****
class Demo4.9{
    public static void main(String[] args){
        for(int j = 0 ; j<5 ; j++){ //控制行数
            for(int i = 0 ; i<5 ; i++){ //控制列数
                System.out.print("*");
            }  // *****         
            System.out.println();//换行       
        }
    }
}

5. Requirements: Print a five-line upright right triangle on the console

*
**
***
****
*****
class Demo4.10{
    public static void main(String[] args){
        for(int i = 0 ; i< 5 ; i++){
            for(int j = 0 ; j<=i ; j++){ //控制列数 
                System.out.print("*");
            }           
            System.out.println();//换行
        }
    }
}

6. Requirement: Print an upside-down five-line right-angled triangle

*****
****
***
**
*
class Demo4.11{
    public static void main(String[] args){
        for(int i = 0 ; i<5;  i++){
            for (int j = 0 ; j<(5-i)  ;j++ ){
                System.out.print("*");
            }
            System.out.println();//换行
        }
    }
}

7. Requirement: print a nine-nine multiplication table

class Demo4.12{
    public static void main(String[] args){
        for(int i = 1 ; i<=9 ; i++){
            for(int j = 1 ; j<=i ; j++){ //控制列数 
                System.out.print(i+"*"+j+"="+i*j+"\t");
            }
            System.out.println();//换行
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325999007&siteId=291194637