Java循环StackOverflowerError-当相同的函数再次出现时,是否有可能继续上一个循环?

Java循环StackOverflowerError-当相同的函数再次出现时,是否有可能继续上一个循环?,java,recursion,Java,Recursion,我有一个递归调用函数的程序。下面的程序给了我堆栈溢出错误 我期望的输出是 正常打印,正常打印,正常打印,正常打印,内部功能splcase now,立即调用动作关键字,正常打印,正常打印,正常打印,正常打印,正常打印 是否有可能控制递归函数以获得所需的输出 public class mytest1 { String path, keyword; public static void main(String args[]){ exec_script("normal

我有一个递归调用函数的程序。下面的程序给了我
堆栈溢出错误

我期望的输出是

正常打印,正常打印,正常打印,正常打印,内部功能splcase now,立即调用动作关键字,正常打印,正常打印,正常打印,正常打印,正常打印

是否有可能控制递归函数以获得所需的输出

public class mytest1 {

    String path, keyword;

    public static void main(String args[]){
        exec_script("normal");
    }

    public static void exec_script(String exec_path){
        for (int i=0; i<10; i++) {
            if (i==4) {
                exec_path = "spl";
            }

            switch (exec_path){
            case "spl":
                spl_case();
                break;
            case "normal":
                System.out.println("normal print");
                break;
            case "call_action_path":
                System.out.println("call action key word now");
                break;
            }
        }
    }

    public static void spl_case(){
        System.out.println("inside function splcase now");
        exec_script("call_action_path");
    }
}
公共类mytest1{
字符串路径,关键字;
公共静态void main(字符串参数[]){
执行脚本(“正常”);
}
公共静态void exec_脚本(字符串exec_路径){

对于(inti=0;i您会得到一个stackoverflow错误,因为您的递归会多次执行该方法,直到堆栈溢出为止

发生的情况是,在从
main
调用该方法后,该方法在第4次迭代中调用
spl\u case
,并执行第2次
exec\u脚本
,然后在第4次迭代中调用
spl\u case
,该过程继续,直到堆栈溢出

以下程序执行您想要的操作:

public class mytest1 {

    String path, keyword; //you have unused variables

    public static void main(String args[]){
        exec_script("normal");
    }

    public static void exec_script(String exec_path){
        for (int i=0; i<10; i++) {
            if (i==4) {
                exec_path = "spl";
            }

            switch (exec_path){
            case "spl":
                spl_case();
                System.out.println("call action key word now");
                exec_path = "normal";
                break;
            case "normal":
                System.out.println("normal print");
                break;
            }
        }
    }

    public static void spl_case(){
        System.out.println("inside function splcase now");
    }
}
公共类mytest1{
字符串路径,关键字;//您有未使用的变量
公共静态void main(字符串参数[]){
执行脚本(“正常”);
}
公共静态void exec_脚本(字符串exec_路径){

对于(inti=0;i,从上面开始,它将持续打印迭代,因此使用下面的代码

因此请使用下面的代码

public class mytest1 {

    String path, keyword;

    public static void main(String args[]){

        exec_script("normal");
    }
    public static void exec_script(String exec_path){
        for (int i=0; i<10; i++){
        if (i==4){
            exec_path = "spl";
        }

        switch (exec_path){
        case "spl":
            spl_case();
            break;
        case "normal":
            System.out.println("normal print");
            break;
        case "call_action_path":
            System.out.println("call action key word now");
            break;

        }
        return;
    }
    }

    public static void spl_case(){
        System.out.println("inside function splcase now");
        exec_script("call_action_path");
    }

}
公共类mytest1{
字符串路径,关键字;
公共静态void main(字符串参数[]){
执行脚本(“正常”);
}
公共静态void exec_脚本(字符串exec_路径){

对于(int i=0;i您将得到stackoverflow错误,因为您的递归将多次执行该方法,直到堆栈溢出

您需要在代码中做以下2个修改才能工作

首先,您需要停止递归。所以,将下面的“return”语句放在switch语句中,而不是“break”语句

case "call_action_path":
            System.out.println("call action key word now");
            return;
}
第二个修改是,

当i==4时,修改“exec\U path”变量值。因此,exec\U path变量值变为“spl”。因此,剩余的连续迭代将执行案例“spl”,因为exec\U path具有“spl”值。因此,在以下情况下,将“exec\U path”变量修改为“normal”

case "spl":
    spl_case();
    exec_path = "normal";
    break;

因此,请执行以下修改并运行它

希望此代码对您有所帮助

public class mytest1 {

    String path, keyword;

    public static void main(String args[]){
        mytest1. exec_script("normal");
    }

    public static void exec_script(String exec_path){
        for (int i=0; i<10; i++){
            if (i==4){
                // exec_path = "spl";
                tree("spl");
            }

            tree(exec_path);

            // switch (exec_path){
            // case "spl":
            //     spl_case();
            //     break;
            // case "normal":
            //     System.out.println("normal print");
            //     break;
            // case "call_action_path":
            //     System.out.println("call action key word now");
            //     break;
            // }
        }
    }

    public static void tree(String exec_path){
        switch (exec_path){
        case "spl":
            spl_case();
            break;
        case "normal":
            System.out.println("normal print");
            break;
        case "call_action_path":
            System.out.println("call action key word now");
            break;
        }
    }

    public static void spl_case(){
        System.out.println("inside function splcase now");
        //exec_script("call_action_path");
        tree("call_action_path");
    }
}
公共类mytest1{
字符串路径,关键字;
公共静态void main(字符串参数[]){
mytest1.exec_脚本(“正常”);
}
公共静态void exec_脚本(字符串exec_路径){

对于(int i=0;ii如果可能,请解释您所做的更改。不鼓励只回答代码,因为它可能会混淆OP和未来的读者。如果我们不返回函数调用,它将继续调用该函数,因此我在函数中添加了return,以在执行开关后停止该执行case@Ricky,你的节目st打印
正常打印
并退出。