2011-04-19 136 views
1

當代碼達到遞歸調用增量時收到錯誤找不到符號,我不知道爲什麼?這是增量的代碼。任何幫助將不勝感激。編譯錯誤:找不到符號

public void increment() { 
    Integer first = 0; 
    Character ch = num.charAt(num.length()-1); 
    Integer last = Character.digit(ch, 10); 

    if (num.length() > 1) 
    { 
     if (last == 9) { 
      last = 0; 
      if (num.length() >= 2) 
      { 
      String temp = new String(num.substring(0, num.length()-2)); 
      temp.increment(); 
      } 
      else 
      { 
      last++; 
      } 
     } 
    } 
    else 
    { 
     if (last == 9) 
     { 
      last = 0; 
      first = 1; 
     } 
     else 
     { 
     last++; 
     } 
    } 

    String t = new String(); 
    String x = new String(); 
    t = last.toString(); 
    x = first.toString(); 

    if (first > 0) 
    { 
    num.concat(x); 
    } 

    num.concat(t); 
} 

編輯:我真的是新來的java所以更基本的,你可以讓你的答案,更好。 好了,所以我收到的錯誤是: BigNatural.java.35:找不到符號 符號法增量() 位置:類java.lang.String temp.increment()

並清理任何這裏的其他問題是整個代碼。

public class BigNatural { 

private String num; 

public BigNatural(String input) { 
    num = input; 
} 


public BigNatural(BigNatural input) { 
    num = input.toString(); 
} 

public BigNatural(Integer input) { 
    num = input.toString(); 
} 

public BigNatural() { 
    Integer i = 0; 
    num = i.toString(); 
} 

public void increment() { 
    Integer first = 0; 
    Character ch = num.charAt(num.length()-1); 
    Integer last = Character.digit(ch, 10); 

    if (num.length() > 1) 
    { 
     if (last == 9) { 
      last = 0; 
      if (num.length() >= 2) 
      { 
      String temp = new String(num.substring(0, num.length()-2)); 
      temp.increment(); 
      } 
      else 
      { 
      last++; 
      } 
     } 
    } 
    else 
    { 
     if (last == 9) 
     { 
      last = 0; 
      first = 1; 
     } 
     else 
     { 
     last++; 
     } 
    } 

    String t = new String(); 
    String x = new String(); 
    t = last.toString(); 
    x = first.toString(); 

    if (first > 0) 
    { 
    num.concat(x); 
    } 

    num.concat(t); 
} 

public void decrement() { 
    Character ch = num.charAt(num.length()-1); 
    Integer last = Character.digit(ch, 10); 

    if(num.length() > 1) 
    { 
     if(last == 0) 
     { 
      String temp = new String(num.substring(0, num.length()-2)); 
      temp.decrement(); 
     } 
     else 
     { 
     last--; 
     } 
    } 
    else 
    { 
     if(last > 0) 
     { 
      last--; 
     } 
     else 
     { 
      last = 0; 
     } 
    } 

    String t = new String(); 
    t = last.toString(); 
    num.concat(t); 
} 


public String toString() { 
    return num; 
} 

} 公共類BigNatural {

private String num; 

public BigNatural(String input) { 
    num = input; 
} 


public BigNatural(BigNatural input) { 
    num = input.toString(); 
} 

public BigNatural(Integer input) { 
    num = input.toString(); 
} 

public BigNatural() { 
    Integer i = 0; 
    num = i.toString(); 
} 

public void increment() { 
    Integer first = 0; 
    Character ch = num.charAt(num.length()-1); 
    Integer last = Character.digit(ch, 10); 

    if (num.length() > 1) 
    { 
     if (last < 9) { 
          last++ 
      } 
      else 
      { 
      last = 0; 
      if (num.length() >= 2) 
      { 
      String temp = new String(num.substring(0, num.length()-2)); 
      temp.increment(); 
      } 
     } 
        else { 
          last++; 
        } 
    } 
    else 
    { 
     if (last == 9) 
     { 
      last = 0; 
      first = 1; 
     } 
     else 
     { 
     last++; 
     } 
    } 

    String t = new String(); 
    String x = new String(); 
    t = last.toString(); 
    x = first.toString(); 

    if (first > 0) 
    { 
    num.concat(x); 
    } 

    num.concat(t); 
} 

public void decrement() { 
    Character ch = num.charAt(num.length()-1); 
    Integer last = Character.digit(ch, 10); 

    if(num.length() > 1) 
    { 
     if(last == 0) 
     { 
      String temp = new String(num.substring(0, num.length()-2)); 
      temp.decrement(); 
     } 
     else 
     { 
     last--; 
     } 
    } 
    else 
    { 
     if(last > 0) 
     { 
      last--; 
     } 
     else 
     { 
      last = 0; 
     } 
    } 

    String t = new String(); 
    t = last.toString(); 
    num.concat(t); 
} 


public String toString() { 
    return num; 
} 

}

+0

發佈整個錯誤以及代碼通常是個好主意:) – forsvarir 2011-04-19 07:43:21

+0

num是一個字符串。 – Bigby 2011-04-19 07:44:47

+0

相關:http://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean – 2014-09-21 02:19:50

回答

6

字符串沒有所謂的增量法。當然,這不是一個遞歸調用,因爲你在一個對象內(在你的代碼中哪個對象沒有類定義),同時你正在調用一個String對象的增量。

此外,您的臨時場永遠不會被使用。 如果你想分享它與方法調用你可以嘗試這樣的事:

public void increment (String temp){} 

,然後通過它,而稱之爲:

String temp = new String(num.substring(0, num.length()-2)); 
increment(temp); 

當然你的函數可以不是這樣的工作。臨時參數應該在你的增量方法中進行管理。審查你的邏輯。這不是語法問題。 如果你不能改變方法簽名,然後宣佈臨時爲您BigNatural類的字段:

public class BigNatural { 

private String num; 
private String temp 
...... 

和增量法裏面簡單地做:

temp = new String(num.substring(0, num.length()-2)); 
+0

我真的很陌生,所以我怎麼接電話呢? – Bigby 2011-04-19 07:43:15

+0

@ user714741:只需寫入:increment()。 (或者this.increment()明確指向這個對象) – Heisenbug 2011-04-19 07:44:10

+0

@Big Brown:如果你是Java新手,我會建議你嘗試Eclipse。這對於開發和學習都是很好的工具,因爲它會立即向您顯示代碼中的錯誤,並提供解決方法。 – Heisenbug 2011-04-19 07:52:09

0

其他一些指針:

字符串是一個不可變的類,一旦它被創建,它就不能再被改變。 所以,做

String t = new String(); 
t = last.toString(); 

現在已經感覺到,因爲你將創建2個字符串對象這裏(last.toString()將創建並返回一個新的字符串)。

簡單地做:

String t = last.toString(); 

甚至更​​好:

num.concat(last.toString()); 

同去的臨時字符串,簡單地做:

String temp = num.substring(0, num.length()-2); 

其次,要注意無意的自動裝箱的:

Integer first = 0; 
first++; 

這會在每次執行first++時創建一個新的Integer對象;整數(as String)是不可變的。 計算時只需使用原始的int而不是Integer

最後,小心不要造成無限循環。如果我理解你的代碼num將連接到num.length() > 1將始終爲真,如果它是第一次。