directory

  • Learning materials
  • methods
  • Method overloading
  • Command line parameter transfer
  • Variable parameter
  • recursive
  • Calculator job

Learning materials

B station crazy god said: ps://www.bilibili.com/video/BV12J…

methods

A method implements only one function, defined in one place and called in many places

Definition syntax: modifiers return value type method name (parameter type parameter name){

. Method body…

Return the return value

}

package com.zy7y.method;

/ * * *@ProjectName: JavaSE
 * @PackageName: com.zy7y.method
 * @Author: zy7y
 * @Date: 2020/8/14 5:44 PM *@Description: * /
public class method01 {
    public static void main(String[] args) {
        int a = 13;
        int b = 1;
        // where a, b are actual parameters
        add(a, b);

    }
    
    // Define an add method that returns an int
    public static int add (int a, int b){
        // a, b form parameters
        returna + b; }}Copy the code

Method overloading

The same class has the same function name but different methods (functions).

Rules for overloading:

  • The method names must be the same.
  • Parameter lists must be different (different number/type/order of parameters, etc.).
  • Methods can have the same/different/return type
  • Just having a different return type is not enough to be a method overload.
package com.zy7y.method;

/ * * *@ProjectName: JavaSE
 * @PackageName: com.zy7y.method
 * @Author: zy7y
 * @Date: 2020/8/14 5:44 PM *@Description: * /
public class method01 {
    public static void main(String[] args) {
        int a = 13;
        int b = 1;
        // where a, b are actual parameters
        int result = add(13.12.13);
        System.out.println(result);

    }

    // Define an add method that returns an int
    public static int add (int a, int b){
        // a, b form parameters
        return a + b;
    }

    // Override the add method: different numbers of arguments
    public static int add (int a, int b, int c){
        return b + a + c;
    }
    // Override the add method: different parameter types
    public static double add (double a, double b){
        returna + b; }}Copy the code

Command line parameter transfer

Variable parameter

It can be one or more

Define syntax: modifiers return value type method name (parameter type… Parameter name) {

. Method body…

Return the return value

}

    public static void sum (int. numbers){
        for (intnumber:numbers) { System.out.println(number); }}Copy the code

The main method

        sum(new int[] {1.3.4.5}); // 1, 3, 4, 5
        sum(1); / / 1
Copy the code

recursive

Call yourself

package com.zy7y.method;

/ * * *@ProjectName: JavaSE
 * @PackageName: com.zy7y.method
 * @Author: zy7y
 * @Date: 2020/8/14 6:26 PM *@Description: recursion * /
public class Recursion {
    public static void main(String[] args) {
        / / factorial
        System.out.println(recursion(5));
    }

    public static int recursion(int number){
        if (number == 1) {
            return 1;
        }else{
            // The factorial of 1 is 1 and the factorial of 5 is 5*4*3*2*1
            return number * recursion(number-1); }}}Copy the code

Calculator job

The course required only loop and switch, but I didn’t have a good implementation method, so I added an if

package com.zy7y.method;

import java.util.Scanner;

/ * * *@ProjectName: JavaSE
 * @PackageName: com.zy7y.method
 * @Author: zy7y
 * @Date: 2020/8/14 6:32 PM *@Description: calculator */
public class Topic {
    / / add
    public static int add(int a, int b){
        return a + b;
    }
    / /
    public static int less(int a, int b){
        return a - b;
    }
    / / by
    public static int product(int a, int b){
        return a * b;
    }
    / / in addition to
    public static int consult(int a, int b){
        return a / b;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        for(;;) { System.out.println("Please select operation method: 1 addition, 2 subtraction, 3 multiplication, 4 division.");
            int choice = scanner.nextInt();
            if(choice ! =1&& choice ! =2&& choice ! =3&& choice ! =4){
                System.out.println("Program quit!");
                System.exit(0);
            }
            System.out.print(Enter the first number:);
            int a = scanner.nextInt();
            System.out.print("Enter the second number :");
            int b = scanner.nextInt();
            switch (choice){
                case 1:
                    System.out.println(add(a,b));
                    break;
                case 2:
                    System.out.println(less(a,b));
                    break;
                case 3:
                    System.out.println(product(a,b));
                    break;
                case 4:
                    System.out.println(consult(a,b));
                    break; }}}}Copy the code