CGB2202 language foundation day 5

Language foundation day 5:

Review:

  1. Scanner receives user data: 3 steps in total

  2. Branch structure:

    • If... else if structure: multiple paths
    • switch... case structure: multiple paths
      Advantages: high efficiency and clear structure
      Disadvantages: you can only judge the equality of integers
      break: jump out of switch
  3. Loop: repeatedly execute a piece of the same or similar code multiple times

  4. Three elements of circulation:

    • Initialization of loop variables
    • Conditions of the loop (based on loop variables)
    • Change of cycle variable (towards the end of the cycle)

      Loop variable: the number that changes repeatedly throughout the loop

  5. Cycle structure:

    • while structure: judge first and then execute. It may not be executed at all
    • do... while structure: execute first and then judge, at least once

      When element 1 is the same as element 3, do... while is preferred

Notes:

  1. Cycle structure:

    • for structure: high application rate and number related cycle
      • Syntax:
        ​ // 1 2 3
        Element (1; for; 3){
        Statement block / loop body ------------- code executed repeatedly 4
        ​ }
      • Execution process:
        ​ 1243243243243243243...2
      • Code demonstration:
        //The loop variable i in the for loop has the scope only in the current for loop
        for(int i=1;i<=9;i++){
            System.out.println(i+"*9="+i*9);
        }
        for(int i=1;i<=9;i+=2){
            System.out.println(i+"*9="+i*9);
        }
        for(int i=9;i>=1;i--){
            System.out.println(i+"*9="+i*9);
        }
        
        for(int times=0;times<5;times++){
            System.out.println("Action is the ladder of success");
        }
        System.out.println("Continue execution...");
        /*
        Execution process:
        times=0  true  output
            times=1  true  output
            times=2  true  output
            times=3  true  output
            times=4  true  output
            times=5  false for End of cycle
            Output continue
        */
        
        //Special format of for: --------- understand
        int i=1;
        for(;i<=9;i++){
            System.out.println(i+"*9="+i*9);
        }
        for(int i=1;i<=9;){
            System.out.println(i+"*9="+i*9);
            i++;
        }
        for(;;){ //An unconditional cycle is an endless cycle
            System.out.println("I want to study...");
        }
        
        for(int i=1,j=5;i<=5;i+=2,j-=2){
        }
        /*
          i=1,j=5  true
          i=3,j=3  true
          i=5,j=1  true
          i=7,j=-1 false
         */
        
  2. Selection rules of three cycle structures:

    • First, check whether the cycle is related to the number of times:
      • If relevant ------------------------------- go directly to for
      • If not, see whether element 1 and element 3 are the same:
        • If the same ----------------------- go directly to do... while
        • If different ----------------------- go directly to while
  3. break: jump out of loop

    for(int i=1;i<=9;i++){
        if(i==4){ //Under certain conditions, end the cycle ahead of time
            break;
        }
        System.out.println(i+"*9="+i*9);
    }
    /* 
      i=1  true  1*9=9
      i=2  true  2*9=18
      i=3  true  3*9=27
      i=4  true
    */        
    

    continue: skip the remaining statements in the loop body and enter the next loop

    //Output the multiplication table of 9, as long as it cannot be divided by 3
    for(int i=1;i<=9;i++){
        if(i%3!=0){
            System.out.println(i+"*9="+i*9);
        }
    }
    //Output the multiplication table of 9 and skip the multiplication table that can be divided by 3
    for(int i=1;i<=9;i++){
        if(i%3==0){
            continue; //Skip the remaining statements in the loop body and enter the next loop
        }
        System.out.println(i+"*9="+i*9);
    }
    /*
        i=1  1*9=9
        i=2  2*9=18
        i=3
        i=4  4*9=36
        i=5  5*9=45
        i=6
        i=7  7*9=63
        i=8  8*9=72
        i=9
        i=10
    */
    
  4. Random adder case:

    package day05;
    import java.util.Scanner;
    //Random adder
    public class Addition {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            int score = 0; //Total score
            for(int i=1;i<=10;i++) { //10 times
                int a = (int)(Math.random()*100); //Addend a, between 0 and 99
                int b = (int)(Math.random()*100); //Addend b
                int result = a+b; //Save the correct answer
                System.out.println("("+i+")"+a+"+"+b+"=?"); //1) Set a topic
    
                System.out.println("Count it!----input-1 Can end in advance");
                int answer = scan.nextInt(); //2) Answer questions
    
                if(answer==-1){ //3) Judgment question
                    break;
                }
                if(answer==result){
                    System.out.println("bingo");
                    score += 10; //If you answer one question correctly, 10 points will be added
                }else{
                    System.out.println("Wrong answer");
                }
            }
            System.out.println("The total score is:"+score);
        }
    }
    
  5. Nested loop:

  6. Array:

Essence notes:

  1. Cycle structure:
    • for structure: high application rate and number related cycle
  2. Selection rules of three cycle structures:
    • First, check whether the cycle is related to the number of times:
      • If relevant ------------------------------- go directly to for
      • If not, see whether element 1 and element 3 are the same:
        • If the same ----------------------- go directly to do... while
        • If different ----------------------- go directly to while
  3. break: jump out of loop
    continue: skip the remaining statements in the loop body and enter the next loop
  4. Nested loop:
  5. Array:

Keywords: Java Algorithm

Added by bobby4 on Fri, 04 Mar 2022 07:20:50 +0200