编译错误:无法找到符号 [英] 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()
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,所以更基本的你的答案,越好。
Ok所以我收到的错误是:
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()
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 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 ++
}
else
{
last = 0;
if(num.length()> = 2)
{
String temp = new String(num.substring(0,num.length()
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;
}
}
解决方案字符串没有名为increment的方法。当然,它不是一个递归调用,因为你是一个对象(在你的代码中没有类定义),同时你正在调用一个String对象的增量。
此外,你的临时字段从不使用。
如果你想在方法调用之间共享它,你可以尝试这样:public void increment ){}
,然后通过调用它:
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)
I am receiving the error cannot find symbol when the code reaches the recursive call for increment and I have no idea why? Here is the code for increment. Any help will be greatly appreciated.
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); }
EDIT: I'm really new to java so the more basic you can make your answers, the better. Ok so the error I am receiving is: BigNatural.java.35: cannot find symbol symbol method increment() location: class java.lang.String temp.increment()
And to clear up any other issues here is the whole code.
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 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++ } 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 has no method called increment. And of course it isn't a recursive call because you are inside an object(which object? in your code there isn't a class definition) , meanwhile you are invoking increment upon a String object.
In addition your temp field is never used. If you want to share it between method calls you can try something like this:
public void increment (String temp){}
and then pass it while calling it:
String temp = new String(num.substring(0, num.length()-2)); increment(temp);
Of course your function can't work like that. temp parameter should be managed inside your increment method. Review your logic. It's no more a matter of syntax. If you can't change the method signature then declare temp as a field of your BigNatural class:
public class BigNatural { private String num; private String temp ......
and inside increment method simply do:
temp = new String(num.substring(0, num.length()-2));
这篇关于编译错误:无法找到符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!