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