Java Basics

Posted by beanwebb on Fri, 07 Jan 2022 22:20:48 +0100

1, java method.

1. Method:
Simple understanding: code blocks that complete specific functions
Methods are defined in many languages, and some languages have the name of functions
In java, a function is a method, and a method is a function.
2. Define the statement format of the method:
Modifier return value type method name (parameter type parameter name 1, parameter type parameter name 2...) {
Function body;
Return return value;
     }

3. Explanation of terms:
(1) modifiers: there are many modifiers, but at present, we only need to remember a combination public static
Since we haven't learned object-oriented knowledge yet, we just need to remember to use it now, which will be explained later.

(2) return value type: refer to the Scanner we have been using
Define the data type returned by return

(3) method name: name the method (must comply with identifier rules)

(4) parameters:
a. formal parameters
The parameter name defined on the method
b. arguments
The value actually passed in by the method to be called in the future

(5) parameter type:
Limit the data types that need to pass in arguments when calling methods in the future

(6) parameter name: (formal parameter)
Is a variable used to receive the arguments passed in by the calling method in the future. When naming, it conforms to the identifier rules, and see the meaning of the name

(7) function body:
The code to realize the function in the future mainly realizes the main logic of the method.

  (8)return:
In general, only methods that have a return value can have a return. Return returns the calculated result to the caller.
Note that the result type returned by return should be consistent with the return value type defined by the method.

(9) return value:
The result returned by the program is returned to the caller.

(10) precautions for use:
a. the method cannot be executed without calling
b. where is the method used? So far, to call a well-defined method, it can only be invoked in the main method.
c. The return value of the method does not need to add parentheses. The role of parentheses is to change the operation order.

public class FunctionDemo1 {
    public static void main(String[] args) {
        //Demand; Calculate two int Sum of types of data
        int a = 10;
        int b = 20;
//        int result = a + b;
//        System.out.println("a+b The sum of is:" + result);
        /**
         *      When a method with a return value is called, generally, there are two processing methods
         *      1,Define a variable to receive the return value of the method. This method is recommended because it is more flexible.
         *      2,Direct output or direct use
         */
        int res1 = sum(a,b);
        System.out.println("a+b The sum of is:" + res1);
        System.out.println("a+b The sum of is:" + sum(a,b));

        //Note: method and method are level relations, and cannot be nested!!!
//        public static int sum1(int a1, int b1) {
//            return a1 + b1;
//        }



    }

    /**
     * Improve with method
     * With return value int
     * Parameters are two data types of int
     */
    public static int sum(int a1, int b1) {
        return a1 + b1;
    }
}

4. Now, all methods have return values. Is there a case where there is no return value?
Yes, when only outputting or writing data, similar to these two cases, the method does not return a value.

There is no return value. What does the return value type write?
java provides a keyword usage: void
It represents a type that has no return value

Example: call the method to print the 99 multiplication table

public class FunctionDemo2 {
    public static void main(String[] args) {
        //Call method to print 99 multiplication table
        printJiujiu();
        System.out.println("=====10000 Want to print after a line of code========");
        printJiujiu();
    }

    /**
     *      Return value type: void no return value
     *      Parameter list: None
     */
    public static void printJiujiu() {
        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();
        }
    }

}

5,

Method overloading: when the method names in a class are the same and the parameter lists are different, this phenomenon is called method overloading.

be careful:
1. Method overloading is independent of the return value
2. When calling, the virtual machine distinguishes the methods with the same name by different parameter lists

public class FunctionDemo3 {
    public static void main(String[] args) {
        //Demand: sum two numbers
        int a = 10;
        int b = 20;
        int res1 = sum(a, b);
        System.out.println(res1);

        //Demand: sum of three numbers
        int c = 30;
        int res2 = sum(a, b, c); //The length of actual parameter list and formal parameter list is different
        System.out.println(res2);

        double d = 40;
        double res3 = sum(a, b, d);
        System.out.println(res3);


    }

    public static double sum(int a, int b, double d) {
        return a + b + d;
    }

    public static int sum(int a, int b, int c) {
        return a + b + c;
    }

    public static int sum(int a, int b) {
        return a + b;
    }

}

6. Enter two data on the keyboard and return the larger of the two numbers

import java.util.Scanner;
public class FunctionTest1 {
    public static void main(String[] args) {
        //Create keyboard entry object
        Scanner sc = new Scanner(System.in);

        System.out.println("Please enter the first data:");
        int num1 = sc.nextInt();
        System.out.println("Please enter the second data:");
        int num2 = sc.nextInt();

        int maxNum = getMax(num1,num2);
        System.out.println("The maximum value is:"+maxNum);

    }

7. Enter two data on the keyboard and compare whether the two numbers are equal

import java.util.Scanner;
public class FunctionTest2 {
    public static void main(String[] args) {
        //Create keyboard entry object
        Scanner sc = new Scanner(System.in);

        System.out.println("Please enter the first data:");
        int num1 = sc.nextInt();
        System.out.println("Please enter the second data:");
        int num2 = sc.nextInt();

        //Call method
        boolean res = isEquals(num1,num2);
        System.out.println(res);
    }

    /**
     *      Defines a method to compare whether two numbers are equal
     *      Return value type boolean
     *      Parameter list int a,int b
     *
     */

    public static boolean isEquals(int a,int b){
        return a==b;
    }
}

 

8. Enter three data on the keyboard and return the maximum value of the three numbers

import java.util.Scanner;

public class FunctionTest3 {
    public static void main(String[] args) {
        //Create keyboard entry object
        Scanner sc = new Scanner(System.in);

        System.out.println("Please enter the first data:");
        int num1 = sc.nextInt();
        System.out.println("Please enter the second data:");
        int num2 = sc.nextInt();
        System.out.println("Please enter the third data:");
        int num3 = sc.nextInt();

        //Call method for comparison
        int maxNum = getMaxNumber(num1,num2,num3);
        System.out.println("The maximum value is:"+maxNum);
    }

    /**
     * Define the method to find the maximum of the three numbers
     * <p>
     * Return value type: int
     * Parameter list: int a,int b,int c
     */
    public static int getMaxNumber(int a, int b, int c) {
        int res1 = (a > b) ? a : b;
        int res2 = (res1 > c) ? res1 : c;
        return res2;
    }
}

9. Enter the number of rows and columns on the keyboard and output the corresponding star

import java.util.Scanner;

public class FunctionTest4 {
    public static void main(String[] args) {
        //Create keyboard entry object
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the number of lines to print:");
        int rowNum = sc.nextInt();

        System.out.println("Please enter the number of columns to print:");
        int colNum = sc.nextInt();

        //Call the method to print the corresponding star
        //When you call a method, you do not need to pass in a data type
        printStar(rowNum, colNum);
    }

    /**
     * Define method to print stars
     * <p>
     * Return value type: void
     * Parameter list: int rowNum,int colNum
     */
    public static void printStar(int rowNum, int colNum) {
        for (int i = 1; i <= rowNum; i++) {
            for (int j = 1; j <= colNum; j++) {
                System.out.print("*\t");
            }
            System.out.println();
        }
    }


}

10. Enter a data n (1 < = n < = 9) on the keyboard and output the corresponding nn multiplication table

import java.util.Scanner;
public class FunctionTest5 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter an integer from 1 to 9:");
        int n = sc.nextInt();
        printjiujiuchengfabiao(n);
    }
    public static void printjiujiuchengfabiao(int n1){
        for (int i=1;i<=n1;i++){
            for (int j=1;j<=i;j++){
                System.out.print(i+"*"+j+"="+(i*j)+"\t");
            }
            System.out.println();
        }
    }
}

 

Topics: Java