编译错误:找不到符号。

3

当代码到达递归调用增量时,我收到了“找不到符号”的错误,但我不知道为什么?这是增量的代码。任何帮助将不胜感激。

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() 位置:类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;
}

}


通常情况下,将整个错误信息和代码一起发布是一个好主意 :) - forsvarir
相关:https://dev59.com/Cl8e5IYBdhLWcg3wfaUy - Stephen C
2个回答

6

String没有名为increment的方法。当然,这不是一个递归调用,因为您在对象内部(哪个对象?在您的代码中没有类定义),同时您正在对一个字符串对象调用increment方法。

此外,您的temp字段从未被使用过。 如果您想在方法调用之间共享它,可以尝试以下方法:

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

我对Java非常陌生,那么我该如何进行调用呢? - Bigby
@user714741:简单地写成:increment()。(或者使用this.increment()来明确引用该对象) - Heisenbug
@Big Brown:如果你是Java新手,我建议你尝试使用Eclipse。它是一个很好的开发和学习工具,因为它可以立即显示代码中的错误,并提供修复建议。 - Heisenbug
我知道那样做可以解决问题,但是评分程序将使用一个测试脚本来测试程序,该脚本看起来会像这样:BigNatural b1 = new BigNatural(34),然后调用b1.increment()。如果我更改increment以接受参数,它将无法与该脚本一起工作,对吧? - Bigby
目前我遇到的问题是,即使在最后一个数字不为9的简单基础情况下也无法进行增量或减量。 - Bigby
显示剩余7条评论

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++都会创建一个新的整数对象;整数(作为字符串)是不可变的。在计算时,使用原始类型int而不是Integer

最后,请注意不要创建无限循环。如果我理解你的代码正确,那么num将被连接到num.length() > 1总是为真,如果第一次为真。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接