2014-11-03 49 views
0

我有一個程序將遞歸調用一個函數。下面的程序給我StackOverflowErrorJava循環StackoverflowError - 當同一個函數再次出現時,是否有可能繼續前一個循環?

我預期的輸出是

normal print, normal print, normal print, normal print, inside function splcase now, call action key word now, normal print, normal print, normal print, normal print, normal print

有沒有可能控制遞歸函數,以便我得到所需的輸出?

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"); 
    } 
} 

回答

0

你得到一個stackoverflow錯誤,因爲你的遞歸繼續執行該方法多次,直到堆棧溢出。

會發生什麼事是,從main調用該方法後,該方法需要在第四迭代spl_case並執行第二exec_script,然後在第四迭代中,它調用spl_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"); 
    } 
} 
-2

它上面不斷付印迭代因此使用下面的代碼。

因此使用下面的代碼

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"); 
    } 

} 
+2

如果可能,請解釋您所做的更改。代碼唯一答案並不真正鼓勵,因爲它可能會混淆OP和未來的讀者。 – 2014-11-03 05:58:25

+0

如果我們沒有返回函數的調用,它將不斷調用該函數,所以我在函數中添加了返回以在執行switch case後停止執行 – Ricky 2014-11-03 06:01:26

+1

@Ricky,您的程序只是打印「正常打印」並退出。 – 2014-11-03 06:05:02

0

你會得到,因爲你的遞歸計算器錯誤繼續執行方法很多次,直到堆棧溢出。

你需要做下面的代碼中的2條的修改工作

首先,你需要停止遞歸。因此,在switch語句而不是「break」語句中將「return」語句放在以下情況中。

case "call_action_path": 
      System.out.println("call action key word now"); 
      return; 
} 

第二變形例是,

你的修改 「exec_path」 變量值時,我== 4。所以,exec_path變量值變成「spl」。因此,由於exec_path具有「spl」值,所以繼續進行迭代,情況「spl」將被執行。因此,修改「exec_path」變到「正常」在以下情況下,

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

所以,做如下修改並運行它

1

月這段代碼可以幫助你。

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"); 
    } 
} 
相關問題