编译错误:找不到符号

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

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;

}

}

回答:

字符串没有称为增量的方法。当然,这不是递归调用,因为您位于一个对象内(哪个对象?在您的代码中没有类定义),同时您在对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));

以上是 编译错误:找不到符号 的全部内容, 来源链接: utcz.com/qa/402238.html

回到顶部