【问题标题】:Compile Error: Cannot Find Symbol编译错误:找不到符号
【发布时间】:2011-04-19 07:39:28
【问题描述】:

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

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

}

【问题讨论】:

标签: java compiler-errors


【解决方案1】:

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

此外,您的临时字段从未使用过。 如果你想在方法调用之间共享它,你可以尝试这样的事情:

public void increment (String temp){}

然后在调用它时传递它:

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

当然你的函数不能那样工作。 temp 参数应在您的增量方法中进行管理。检查你的逻辑。这不再是语法问题。 如果您无法更改方法签名,则将 temp 声明为 BigNatural 类的字段:

public class BigNatural {

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

内部增量方法只需这样做:

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

【讨论】:

  • 我真的是 java 新手,那我该怎么打这个电话呢?
  • @user714741 :只需写: increment() 。 (或 this.increment() 用于显式引用此对象)
  • @Big Brown:如果您是 Java 新手,我建议您尝试 Eclipse。它是开发和学习的好工具,因为它会立即向您显示代码中的错误,并提供修复建议。
  • 我想我只是有点困惑,因为我知道测试脚本调用将涉及 b1.increment() 之类的东西,而 b1 是 BigNatural。另外,如果我只是调用 increment(),temp 将如何成为正在处理的新字符串。
  • @Big Brown:我没有完全理解你。无论如何,在当前隐式对象(this)上调用增量。如果您需要传递一个参数,请更改签名,如:void implement(String s)
【解决方案2】:

其他一些指针:

String 是一个不可变的类,这意味着一旦创建它就不能再更改。 所以,做

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() &gt; 1 如果第一次为真,则永远为真。

【讨论】:

    猜你喜欢
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    相关资源
    最近更新 更多