public static void main(String[] args) {
int total = 0; //sum
total = sum(10);
System.out.println(total);
}
// Implement recursion from 1 + 2 + 3 +...+ 10
public static int sum(int n){
System.out.println(n);
if(n==1){
return n; // exit
}else{
return n + sum(n-1);
}
}
2. Recursively implement a+ (a+1) + (a+2) +...b . Where a and b are integers not less than 1, and b is the maximum value of all addends
public static void main(String[] args) {
int total = 0; //sum
total = sum(5,10);
System.out.println(total);
}
//Recursive implementation a+ (a+1) + (a+2) +...+ b //where a and b are integers not less than 1, and b is the maximum value of all addends
public static int sum(int a, int b){
System.out.println(b);
if(a==b){
return a; // exit
}else{
return b + sum(a, b-1);
}
}
3. Delete the specified directory (including all subdirectories and files under the directory)
public static void main(String[] args) {
delete(dir);
}
/**
* The method to delete the file and directory represented by the given File
*/
public static void delete(File dir){
if(dir.isDirectory()){ //Determine whether dir is a directory, if it is a file, delete it directly
File[] subs = dir.listFiles(); //Get an array of the names of all directories and files in the dir directory
for(File sub : subs){ // Traverse the dir directory and get the names of all the contents in the dir directory
delete(sub); //Recursive call (when the directory is empty, it is the exit)
}
dir.delete(); //delete directory
System.out.println("Delete directory: "+dir);
}else{
dir.delete(); //delete file
System.out.println("Delete file: "+dir);
}
}