尝试创建一个构造函数,该构造函数将使用参数中给定的数字初始化 'number' 并设置长度
Trying to create a constructor that will initialize 'number' with a number given in an argument and set the length
正在尝试在 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)
在您的 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' 并设置长度。
我设置一个整数'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)
在您的 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;
}