2013-04-05 72 views
1

我有一個int,在每次遞歸之後添加一個「count」,但我也有一個if語句,一旦int等於或大於另一個整數,就會停止遞歸。不知何故,如果陳述被忽略。有人可以解釋爲什麼我遇到了stackoverflow?

public static boolean wildcard(String x, String y, int substring, int count) { 
    if (count >= y.length()){ 
     System.out.println("asdf"); 
     return true; 
    } 

    if (x.charAt(count) == y.charAt(count)){ 
     System.out.println("ALSKDFJKL"); 
     return wildcard(x, y, substring, count++); 
    } 
    if (y.charAt(count) == '*'){ 
     return wildcard(x.substring(substring), y, substring++, count); 


    System.out.println("wildcard end"); 
    return false; 
    } 
+0

你怎麼知道'if'語句被忽略? – Floris 2013-04-05 03:50:46

+0

,因爲如果它不被忽略,遞歸會結束 – user1692517 2013-04-05 03:51:50

+0

*有人可以解釋爲什麼我遇到stackoverflow?* - 因爲你有一個編程問題,你需要幫助回答。 – jxh 2013-04-05 22:44:26

回答

5

而不是return wildcard(x, y, substring, count++);嘗試return wildcard(x, y, substring, ++count);

count++是後增量(這意味着它將增加該方法返回後)

你可能也需要更新return wildcard(x.substring(substring), y, substring++, count);出於同樣的原因。

而且,你最後if語句被分解......我覺得System.outreturn false想成爲if塊外

+0

修好了!謝謝 – user1692517 2013-04-05 03:50:28

相關問題