簡體   English   中英

編譯錯誤:找不到符號

[英]Compile Error: Cannot Find Symbol

當代碼到達遞增調用時我收到錯誤找不到符號,我不明白為什么? 這是增量代碼。 任何幫助將不勝感激。

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:找不到符號符號方法increment()location:class 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;
}

}

String沒有名為increment的方法。 當然,它不是一個遞歸調用,因為你在一個對象內(在你的代碼中哪個對象?沒有類​​定義),同時你在String對象上調用增量。

此外,您的臨時區域從未使用過。 如果你想在方法調用之間共享它,你可以嘗試這樣的事情:

public void increment (String temp){}

然后在調用它時傳遞它:

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

當然,你的功能不能像那樣工作。 temp參數應該在increment方法中管理。 檢查你的邏輯。 這不再是語法問題。 如果您無法更改方法簽名,則將temp聲明為BigNatural類的字段:

public class BigNatural {

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

和內部增量方法簡單地做:

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

其他一些指示:

String是一個不可變類,意味着一旦創建它就不能再被更改了。 所以,做

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

現在已經有了意義,因為你將在這里創建2個String對象( last.toString()將創建並返回一個新的String)。

簡單地說:

String t = last.toString();

甚至更好:

num.concat(last.toString());

臨時字符串也是如此,只需:

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

接下來,請注意無意識的自動裝箱:

Integer first = 0;
first++;

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

最后,請注意不要創建無限循環。 如果我理解你的代碼num將連接到所以num.length() > 1將是真如果它第一次是真的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM