Java method 01 (detailed method, recursion)

What is a method? A
Java method is a collection of statements that perform a function together.
1. A method is an orderly combination of steps to solve a type of problem.
2. The method is contained in a class or object.
3. The method is created in the program and in other
The principle of the design method is cited in the place: the original meaning of the method is a function block, which is a collection of sentence blocks that realize a certain function. When we design a method, it is best to maintain the atomicity of the method, that is, a method only completes one function, which is conducive to our later expansion.
Structure :
Modifier return value type method name (parameter type parameter name...) { method body...return return value (if there is no return value, don’t write it)} Code example (combination) (similar to a custom function in c language)





package com.hao.method;

public class Demo01 {
    public static void main(String[] args) {
        int sum=add(1,2);
        System.out.println(sum);
    }
    public static int add (int a,int b){
        return a+b;
    }

}

Output sample
Insert picture description here
code example

package com.hao.method;

import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {
        int a;
        int b;
        int result=-1;
        System.out.println("请输入你要比较的两个数:");
        Scanner scanner = new Scanner(System.in);
        a=scanner.nextInt();
        b=scanner.nextInt();
        result=max(a,b);
        System.out.println(result);

    }
    //比大小
    public static int max(int num1,int num2){
       return num1>=num2?num1:num2;//如果num1>=num2输出num1,否则输出num2
    }
}


Insert picture description here
Overloading
rules of the output example method
1. The name of the method must be the same
2. The parameter list must be different (number, type or parameter arrangement order)
3. The return type of the method can be the same or different
4. Only the return value type is not enough Become a method overload
code example

package com.hao.method;

import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {
        int a;
        int b;
        double c;
        double d;
        int result=-1;
        double result2=-1.0;
        System.out.println("请输入你要比较的两个整数:");
        Scanner scanner = new Scanner(System.in);
        a=scanner.nextInt();
        b=scanner.nextInt();
        result=max(a,b);
        System.out.println("最大值为:");
        System.out.println(result);
        System.out.println("请输入你要比较的两个浮点数:");
        c=scanner.nextDouble();
        d=scanner.nextDouble();
        result2=max(c,d);
        System.out.println("最大值为:");
        System.out.println(result2);

    }
    //比大小
    public static int max(int num1,int num2){
       return num1>=num2?num1:num2;
    }
    public static double max(double num1,double num2){
        return num1>=num2?num1:num2;
    }
}

Output example
Insert picture description here
recursion
1. Recursion means A method calls A method
2. The recursive structure consists of two parts:
1) Recursion header: when not to call its own method. If there is no head, it will fall into an endless loop.
2) Recursive body: When do you need to call your own method
code example

package com.hao.method;

public class Demo06 {
    public static void main(String[] args) {
        System.out.println(f(5));
    }
    public static int f(int N){
        if(N==1){
            return 1;
        }else{
            return N*f(N-1);
        }
    }
}

Sample output
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_51224492/article/details/111321669