我收到的错误找不到符号,当代码到达递增的递增调用时我不明白为什么?这是增量代码。任何帮助将不胜感激。
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:找不到符号
符号方法增量()
location:类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;
}
}
答案 0 :(得分:6)
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));
答案 1 :(得分:0)
其他一些指示:
String是一个不可变类,意味着一旦创建它就不能再被更改了。 所以,做
String t = new String();
t = last.toString();
现在已经有了意义,因为你将在这里创建2个String对象(last.toString()
将创建并返回一个新的String)。
简单地说:
String t = last.toString();
甚至更好:
num.concat(last.toString());
同样适用于temp String,只需执行:
String temp = num.substring(0, num.length()-2);
接下来,请注意无意识的自动装箱:
Integer first = 0;
first++;
每次执行first++
时都会创建一个新的Integer对象;整数(作为String)是不可变的。
计算时只需使用原始int
而不是Integer
。
最后,请注意不要创建无限循环。如果我理解你的代码num会连接到那么num.length() > 1
将是真的,如果第一次是真的那么。