1 Public classHelloWorld {2 Public Static voidMain (string[] args) {3 4 //Create object, object named Hello5HelloWorld Hello =NewHelloWorld ();6 7 //call the method and pass in the scores of the two courses8Hello.calcavg (94, 81);9 }Ten One /* A * Function: Calculate the average score of two courses test results and output the average score - * Define a method that contains two parameters to pass the scores of two courses - */ the Public voidCalcavg (intScore1,intscore2) { - Doubleavg; -Avg = (score1 + score2)/2.0; - System.out.println (avg); +}
Source: http://www.imooc.com/code/1580
Sometimes the execution of a method depends on certain conditions, in other words, it is necessary to provide additional information to accomplish a particular function through the method. For example, the real-life rice cooker can achieve the function of "cooking rice", but only if we have to provide the ingredients, if we do not provide anything, it is really "paddle". we can receive external incoming data information by adding a parameter list to the method, which can be any basic type data or reference type data.
Let's take a look at a method with parameters but no return value:
The code above defines a show method with a parameter name, which implements the output welcome message.
Calling the parameter method is similar to invoking the syntax of the parameterless method, but must pass in the actual parameter value when called
For example:
Operation Result:
Many times, we refer to the parameter defining the method as a formal parameter, which is used to define the number and type of parameters that the method needs to pass in, and to call the method when the argument is called an argument, which is the value passed to the method that is actually processed.
A question that must not be overlooked:
1. When calling the parameter method, the number, type, and order of the arguments must be guaranteed to correspond to the parameter one by one.
2. When calling a method, the argument does not need to specify a data type, such as
3, the parameters of the method can be basic data types, such as int, double, etc., can also be reference data type, such as String, array, etc.
4, when the method parameter has multiple, multiple parameters are separated by commas
Task
Please fill in the Complete method code after the first line in the editor
Realization function: Write the parameter method to calculate the average score of two courses test results, pass the method parameters to the score information
Operation Result:
1 Public classHelloWorld {2 Public Static voidMain (string[] args) {3 4 //Create object, object named Hello5HelloWorld Hello =NewHelloWorld ();6 7 //call the method and pass in the scores of the two courses8Hello.calcavg (94, 81);9 }Ten One /* A * Function: Calculate the average score of two courses test results and output the average score - * Define a method that contains two parameters to pass the scores of two courses - */ the - - - + - + A at - - - - - in -}
Myans:
Mu class net-java first season -7-5 the use of no return value method in Java