If you implement multiple methods, the logic of these methods is basically the same, the only difference is the number of parameters passed, you can use the variable parameter
Variable parameter definition method data type ... The name of the array, which stores the arguments passed over, similar to JavaScript
Note the point:
(1) Variable parameters need to be written in the parameter list of the method and cannot be defined separately
(2) There can be only one variable parameter in the parameter list of a method
(3) A variable parameter in the parameter list of the method, which must be placed at the end of the argument
-Score (String name, int ... Args
Case
Public classChangeableargs { Public Static voidMain (string[] args) {score ("Xiaoming", 90,80,70,90); Score ("Little Red", 90,80,70,90,100,200,300); System.out.println (Add (90,80,70,90)); System.out.println (Add (90,80,70,90,100,200,300)); } Private Static voidScore (String name,int.. args) { intResult=0; for(inti=0;i<args.length;i++) Result+=Args[i]; SYSTEM.OUT.PRINTLN (Name+ "Test the +result+" points! "); } Private Static intAddint.. args) { intResult=0; for(inti=0;i<args.length;i++) Result+=Args[i]; returnresult; }}
Run:
Java variable parameters explained