文件搜索-方法递归

递归是一种算法,在程序设计语言中广泛应用,从形式上说,方法调用自身得形式称为方法递归

递归的形式:

直接递归:方法自己调用自己

间接递归:方法调用别的方法,而别的方法又调用自己

package com.file.test;

public class RecursionTest {
//    直接递归
    public static void main(String[] args) {
        test1();
    }

    public static void test1(){
        System.out.println("Test1");
            test1();
            }


//间接递归
            public static void test2(){
                System.out.println("test2");
                test3();
            }
            public static void test3(){

                test2();
            }
}


案例:计算一个数的阶乘

package com.file.test;

import java.util.Scanner;

public class Ntest {

    public static void main(String[] args) {

        System.out.println("计算N的阶乘");
        System.out.println("请输入N的值");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.close();
        int x=1;
        int y=1;
        while (x<=n) {
          y=y*n;
          n--;

        }
        System.out.println("N的阶乘为"+y5);
    }
}

利用递归来解决阶乘问题:

package com.file.test;

import java.util.Scanner;

public class Ntest {

    public static void main(String[] args) {

//        System.out.println("计算N的阶乘");
//        System.out.println("请输入N的值");
//        Scanner sc = new Scanner(System.in);
//        int n = sc.nextInt();
//        sc.close();
//        int x=1;
//        int y=1;
//        while (x<=n) {
//          y=y*n;
//          n--;
//
//        }
//        System.out.println("N的阶乘为"+y);
        System.out.println("利用方法递归来实现阶乘");
       Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        int f = f(i);
        System.out.println("阶乘为:"+f);


    }
    public static int f(int n){

        if(n==1){
            return 1;

        }else {
            return f(n-1)*n;

        }

    }

}

文件搜索案例:

package com.file.test;


import java.io.File;

public class uninstDemo {

    public static void main(String[] args) {
        //在文件D盘下找出uninst.exe文件所在位置
        
        SearchUninst(new File("D:/"),"uninst.exe");
        
    }
    /**
     *
     *
     * 目录下搜索某个文件
     *
     * dir :目录
     *
     * fileName:文件名称
     *
     */
    public static void SearchUninst(File dir, String fileName) {

        if(dir==null || !dir.exists() || !dir.isDirectory()) {

            System.out.println("非法参数,无法搜索");
            return;

        }
        //dir不是null,存在,是目录
        // 获取文件目录下的所有一级文件对象
        //遍历一级文件对象
        File[] files = dir.listFiles();//得到一个一级文件对象数组
        //判断当前目录下是否存在一级文件对象,或者是否可以拿到一级文件对象
        if(files!=null && files.length>0) {
            for (File file : files) {
                //判断文件是否是文件或者文件夹
            if(file.isFile()){
                //判断是否是我们要找的文件
                if(fileName.equals(file.getName())) {
                    System.out.println("找到了"+file.getAbsolutePath());
                }
            }

            if(file.isDirectory()){
                SearchUninst(file, fileName);
            }

            }

        }

    }

}

博客内容均系原创,未经允许严禁转载!
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇