尝试在 java 中创建一个构造函数,该构造函数将使用参数中给出的数字初始化“number”并设置长度。
我将整数“l”设置为 number.length 并设置 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)
最佳答案
在您的 cod 中,您正在使用 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;
}
关于java - 尝试创建一个构造函数,该构造函数将使用参数中给出的数字初始化 'number' 并设置长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58161318/