Java basic grammar (2) --- process control, function

Java basic grammar (2) - flow control, function


1. Process control

1.1 Judgment structure (if statement)


Three expressions of if statement:
a) Format 1:

    if(条件表达式)
    {
        执行语句;
    }

b) Format 2:

    if(条件表达式)
    {
        执行语句;
    }
    else
    {
        执行语句;
    }

c) Format three:

    if(条件表达式)
    {
        执行语句;
    }
    else if(条件表达式)
    {
        执行语句;
    }
    ……
    else
    {
        执行语句;
    }

1.2 Selection structure (switch statement)

Format:

        switch(表达式)
        {
            case取值1
                执行语句;
                breakcase取值2
                执行语句;
                break
                ……
            default
                执行语句;
                break
        }
  • Features
    1. There is no order between case and default. The first case is executed first, and the default is executed if there is no matching case.
    2. There are two situations to end the switch statement: (a) encounter break; (b) execute to the end of the switch statement.
    3. If the matching case or default does not have a corresponding break, then the program will continue to execute downward, running executable statements until it encounters a break or the end of the switch.
    4. After entering the switch statement, the execution order is to execute the case first, then from top to bottom, and finally execute the default. Even if default is placed above the case, the execution order does not change.

1.3 Loop structure

The format of the while statement:

    while(条件表达式)
    {
        执行语句;
    }

do while statement format:

    do
    {
         执行语句;
    }while(条件表达式);

for statement format:

    for(初始化表达式;循环条件表达式;循环后的操作表达式)
    {
        执行语句;(循环体)
    }

2. Function

  • Definition of a function: A function is an independent small program defined in a class with a specific function. A function is also called a method.
  • The format of the function:
        修饰符 返回值类型 函数名(参数类型 形式参数1,参数类型 形式参数2,……)
        {
            执行语句;
            return 返回值;
        }
  • Return value type: The data type of the result after the function runs.
  • Parameter type: is the data type of the formal parameter.
  • Formal parameter: is a variable used to store the actual parameters passed to the function when the function is called.
  • Actual parameter: The specific value passed to the formal parameter.
  • return: used to end the function.
  • Return value: The result of the function operation, the result will be returned to the caller.
  • Features of the function
    1. Defining functions can encapsulate functional code.
    2. It is convenient to reuse this function.
    3. A function is only executed when it is called.
    4. The appearance of functions improves the reusability of code.
  • function overloading
    • The concept of overloading:
      In the same class, more than one function with the same name is allowed, as long as they have different parameter numbers or parameter types.
    • The benefits of overloading:
      convenience and reading, optimized program design.

3. Practice

3.1 Printing a Right Triangle

  • Requirement: Print the following right triangle

    *****
    ****
    ***
    **
    *
    
  • Ideas:

    1. Using for nested loops
    2. Outer control line number
    3. The number of inner control columns (the number of columns is a variable, decremented by 1 each time)
  • code:

    class ForForDemo 
    {
        public static void main(String[] args) 
        {
            for(int x=0;x<5;x++)
            {
                //列变量用行变量表示,外层每循环一次,y初始值加1;
                for(int y=x;y<5;y++)
                {
                    System.out.print('*');
                }
                    System.out.println();
            }
        }
    }
  • Output result:

3.2 Print the digital triangle:

  • Requirement: Print the following digital triangle

    54321
    5432
    543
    54
    5
    
  • Ideas:
    1. Outer control line number
    2. The number of inner control columns (the number of columns is a variable, decremented by 1 each time)
    3. The numbers printed on each line are arranged from largest to smallest (represented by the number of columns)
  • code:
    class ForForDemo1 
    {
        public static void main(String[] args) 
        {
            for(int x=1;x<=5;x++)
            {
                for(int y=5; y>=x;y--)
                {
                    //打印的数字大小可以用y表示
                    System.out.print(y);
                }
                System.out.println();
            }           
        }
    }
  • Output result:

3.3 Obtain the grade corresponding to the student's score according to the test score

  • Requirements: Give corresponding grades according to students' scores;

    90~100 A
    80~89  B
    70~79  C
    60~69  D
    60以下  E
    
  • Ideas:

    1. Determine the grade corresponding to the student's score by the if statement
    2. Print out student grades with an output statement
  • code:

   class  FunctionDemo
    {
        public static void main(String[] args) 
        {
            sop(getLevel(89));
        }
        //定义一个方法获取学生分数对应的等级
        public static char getLevel(int grade)
        {
                //90~100分返回等级A;
                if(grade>=90 & grade<=100)
                    return 'A';
                //80~89分返回等级B;
                else if(grade>=80 & grade<=89)
                    return 'B';
                //70~79分返回等级C;
                else if(grade>=70 & grade<=79)
                    return 'C';
                //60~69分返回等级D;
                else if(grade>=60 & grade<=69)
                    return 'D';
                //60分以下返回等级E;
                else
                    return 'E';
        }
        //将输出语句进行封装
        public static void sop(Object obj)
        {
            System.out.println(obj);
        }
    }
  • The result of the output:

Guess you like

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