试图创建一个构造函数,该构造函数将使用参数中给出的数字来初始化'number'并设置长度

问题描述 投票:0回答:1

[尝试在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)
java string constructor biginteger
1个回答
0
投票

在您的鳕鱼中,您正在使用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;
}
© www.soinside.com 2019 - 2024. All rights reserved.