java - 编译错误 : Cannot Find Symbol

标签 java compiler-errors

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

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 温度增量()

要在这里解决任何其他问题的是整个代码。

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

} 公共(public)课 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));

关于java - 编译错误 : Cannot Find Symbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5713110/

相关文章:

android - 错误 :Execution failed for task ':app:transformClassesWithJarMergingForDebug' in android + volley libraries

java - 有条件地向 Java 8 流添加操作

android - 生成的渲染脚本文件导致 “Invalid unicode”错误

Java : Socket programming example

c++ - VS 2015 的 HYPRE blas 构建错误

c++ - 使用extern时C++中的重新声明错误

java - 使用 java 迭代 tar.gz 文件

java - DriverManager.registerDriver() 静态方法

java - 使用 for 循环填充数组时遇到问题 - java

java - NetBeans GUI 设计器窗口在设计和源代码之间切换不显示