Class notes on June 12, 2021
Command line parameters
- Sometimes you want to pass a message to a program when it runs. This is achieved by passing command line parameters to the main() function.
package com.kuang.method; public class Demo03 { public static void main(String[] args) { //args.length array length for (int i = 0; i < args.length; i++) { System.out.println("args["+i+"]:"+args[i]); } } }
cd... / back up
There is no need to write the package name for compilation, and the package name must be written for execution
Variable parameter / indefinite parameter
- JDK1. Starting from 5, java supports passing variable parameters of the same type to a method
- In the method declaration, add an ellipsis (...) after specifying the parameter type
- Only one variable parameter can be specified in a method. It must be the last parameter of the method. Any ordinary parameter must be declared before it
package com.kuang.method; public class Demo04 { public static void main(String[] args) { Demo04 demo04 = new Demo04(); demo04.test(1,2,3,4,5,6); } public void test(int... i){ System.out.println(i[0]); System.out.println(i[1]); System.out.println(i[2]); System.out.println(i[3]); System.out.println(i[4]); System.out.println(i[5]); } }
printMax(…numbers:34,3,3,2,56.5); Always report errors!!!
Variable parameter call example
package com.kuang.method; public class Demo041 { public static void main(String args[]) { //Call methods with variable parameters printMax(new double[]{1,2,3}); } public static void printMax(double... numbers){ if(numbers.length == 0){ System.out.println("No argument passed"); return; } double result = numbers[0]; //Take the maximum value for (int i = 1; i < numbers.length; i++) { if (numbers[i] > result){ result = numbers[i]; } } System.out.println("The max value is " + result); } }
Recursion (key and difficult points)
- Method A calls method B, which is easy to understand
- Recursion means that method A calls method A and calls itself
- Using recursion, we can solve some complex problems with simple programs. It usually transforms a large and complex problem layer by layer into a smaller problem similar to the original problem to solve. The recursive strategy only needs a small amount of program to describe the multiple repeated calculations required in the problem-solving process, which greatly reduces the amount of code of the program. The ability of recursion is to define an infinite set of objects with limited statements
- Recursive structure
- Recursive header: when not to call its own method. If there is no head, it will fall into a dead cycle
- Recursive body: when do I need to call my own method
Recursive example
package com.kuang.method; public class Demo06 { public static void main(String[] args) { System.out.println(f(5)); } //1!=1 //2!=2*1 //5!=5*4*3*2*1 //Boundary conditions, such as f(1) //Pre stage //Return phase //Stack mechanism //Small amount of calculation can use recursion, considering the computational power! public static int f(int n){ if(n==1){ return 1; }else{ return n*f(n-1); } } }
Method call example
package com.kuang.method; public class Demo041 { public static void main(String args[]) { //Call methods with variable parameters printMax(new double[]{1,2,3}); } public static void printMax(double... numbers){ if(numbers.length == 0){ System.out.println("No argument passed"); return; } double result = numbers[0]; //Take the maximum value for (int i = 1; i < numbers.length; i++) { if (numbers[i] > result){ result = numbers[i]; } } System.out.println("The max value is " + result); } }
Simple calculator (addition, subtraction, multiplication and division of two numbers)
package com.kuang.method; import java.util.Scanner; public class Calculator { public static void main(String[] args) { //User interactive keyboard input Scanner scanner = new Scanner(System.in); double sum =0; System.out.println("Please enter the first number:"); double num1 = scanner.nextDouble(); System.out.println("Please enter the operation symbol:"); String operator = s canner.next(); System.out.println("Please enter the second number:"); double num2 = scanner.nextDouble(); //Defining variables: operation symbols //The addition, subtraction, multiplication and division are respectively corresponding to the operation process switch (operator){ case "+": sum = num1 + num2; break; case "-": sum = num1 - num2; break; case"*": sum = num1 * num2; break; case"/": sum = num1 / num2; break; } System.out.println(sum); } }