[尝试在Java中创建一个构造函数,该构造函数将使用参数中给出的数字初始化'number'并设置长度。
我将整数'l'设置为number.length并设置长度= l
public BigInteger(String num){
int l = number.length;
for(int i=0;i<l;i++)number[i] = num[i];
length = l;
}
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The type of the expression must be an array type but it resolved to String
Type mismatch: cannot convert from String to char
at BigInteger.<init>(BigInteger.java:16)
at BigInteger.main(BigInteger.java:322)
在您的鳕鱼中,您正在使用num[i]
。虽然num
不是array
类型。它的类型为String
。看起来number
的类型为char[]
。因此,您的代码应类似于:
public BigInteger(String num){
int l = number.length;
for(int i=0;i<l;i++) {
//Change is here
number[i] = num.charAt(i);
}
length = l;
}