javaDay 06 method call

Method call

Common shortcut keys of IDEA

method

Modifier: current fixed writing method, public static

Return value type: that is, what type of file the method will eventually generate

Method name: the name of the method, rules and variables are the same, try to use the small hump format

Parameter type: what type of data is entered into the method

Parameter name: the name of the variable corresponding to the data entered into the method. If there are multiple parameters, they can be separated easily

Method body: what the method needs to do, some code

return: ①: stop the current method
② : return the following to the call
The type of return value must be consistent

Return value: that is, the data result that will last long after the method is executed

Difference of return value or not

Three formats of method call


Individual call: method name (parameter);
Print call: system out.println (method name (parameter));
Assignment call: data type variable name = method name (parameter);

	public static void main(String[] args) {
    //Call individually
    sum(10,15);//sum has a value of 25, but no output
    System.out.println("==================");
    //Print call
    System.out.println(sum(15,20));//Among them, a and b are not typed by hand, but are automatically prompted by the compiler
    System.out.println("===================");
    //Assignment call
    int number = sum(20,25);
    System.out.println(number);
}
public static int sum (int a, int b)
{
    System.out.println("Method executed!");
    int result = a+b;
    return result;
}

Method considerations


1: Methods should be defined in classes, but methods cannot be defined in methods, and cannot be nested
2: It doesn 't matter the order of method definitions
3: Method will not be executed after definition. If you want to execute, you must call, call separately, print and assign
4: If the method has a return value, you must negotiate the return value of "return". It is not allowed to have no return value
5: The return value data after return must correspond to the return value type of the method
6: For a method that has no return value for void, you cannot write the return value after return. You can only write return yourself
7: For the last return line in the void method, you can omit it
8: You can have multiple return statements in a method, but you must ensure that only one of your colleagues will be executed. Two return statements cannot be written together

Call print method

//call
public static void main(String[] args) {
    printmethod();
}

public static void printmethod() {
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 20; j++) {
            System.out.print("*");
        }
        System.out.println();
    }

}

Drawing explanation

Method with and without parameters

//Code with or without parameters
public static void main(String[] args) {
       method1(10,20);
       method2();
}
//With parameters
public  static  void method1(int a,int b){
    int result = a*b;
    System.out.println(result);
}
//Print ten fixed strings
public  static  void method2(){
    for (int i = 0; i < 10; i++) {
        System.out.println("HelloWorld!"+i);
    }
}

public static void main(String[] args) {
        printsum(10, 20);//Generally speaking, this value cannot be assigned
      /* int numer = printsum(10,20);
        System.out.println(number);*/
//        An error will be reported here because your printsum type is void and has no return value, which is equivalent to assigning a value to something that is nothing
//        So there will be no results, no output
        System.out.println(getsum(15, 20));//Print call, this value can be assigned
        int number = getsum(20, 25);//Assignment call
        System.out.println(number);
        getsum(25,30);//Although it is written correctly, the return value is not called, so it will not be displayed
    }
    //A method is responsible for adding two numbers. Whoever calls me will tell who the return value is
    public static int getsum(int a, int b) {
        int result = a + b;
        return result;
    }
    //One method, which is responsible for adding two numbers, has no return value, the result will not tell people, and can only be printed out
    public static void printsum(int a, int b) {
        int result = a + b;
        System.out.println("The result is:" + result);
        }

practice

Judge whether two numbers are the same

//Exercise 1: judge whether the two numbers are the same
public static void main(String[] args) {
    System.out.println(issame(10, 20));
}

public static boolean issame(int a, int b) {
   /* boolean same;
    if (a == b) {
        same = true;
    } else {
        same = false;
    }*/
    //The first method is above, and the next method is return same;
    //boolean same = a==b;
    //The second method is return same;
    //boolean same = a == b? true : false;
    //The third method is return same;
    return a == b;
    //Directly return a==b; because here a==b, only two of them are true or false, and no other value will appear
}

Exercise 2: define the method and find the sum of one to one hundred

//Exercise 2: define the method and find the sum of one to one hundred
public static void main(String[] args) {
    System.out.println(getsum());
}
public  static int getsum(){
    int sum= 0;
    for (int i = 0; i < 100; i++) {
        sum+=i;
    }
    return sum;
}

Exercise 3: let the computer output the specified number of helloworld!

//Exercise 3: let the computer output the specified number of helloworld!
public static void main(String[] args) {
            printcount(10);
}

public static void printcount(int num) {
    for (int i = 0; i < num; i++) {
        System.out.println("helloworld!" + i);
    }
}

Posted on Sat, 13 Jun 2020 01:41:38 -0400 by kevinjo