【245天】我愛刷題系列(4)

叨叨兩句

  1. 今天好忙啊,但很爽,打造最強本身的感受真好。

題12: 遞歸求階乘

package com.test;

public class Test01 {

    public static void main(String[] args) {
        System.out.println(func(5));
    }
    
    //該方法用於求解i的階乘
    /*5! = 5 * 4!
     *            4!= 4 * 3!
     *                     3!= 3 * 2!
     *                              2! = 2 * 1!
     *                                       1! = 1;    
    */
    
    public static int func(int i) {
        if(i == 1) {
            return 1;
        }
        
        return i * func(i - 1);
    }
    
}

題13:遞歸打印文件夾中全部文件路徑

package com.test;

import java.io.File;

public class Test01 {

    public static void main(String[] args) {
        printSubFileName(new File("C:\\Users\\Dell xps\\Desktop\\待讀書單(深度)——編程"));
    }
    
    public static void printSubFileName(File file) {
        File[] files = file.listFiles();
        for(File subFile : files) {
            if(subFile.isFile()) {
                System.out.println(subFile.getAbsolutePath());
            } else {
                printSubFileName(subFile);
            }
        }
    }
    
}
相關文章
相關標籤/搜索