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

标签 java string constructor biginteger

尝试在 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/

相关文章:

java - 找不到合适的驱动程序 Postgres JDBC

javascript - 查找至少两个字符串中的唯一值

c++ - QF模式匹配算法伪代码转c代码

包含数据库查询的 PHP 构造函数来构建对象,好/坏?

javascript - 为什么这个属性是在原型(prototype)上定义的,而不是在构造函数上?

java - Mockito框架

java - 将包含符号链接(symbolic link)的 Java 源文件夹添加到 build.sbt 文件

java - throws 语句不考虑继承异常

java - 从字符串中获取与正则表达式匹配的未知值的字符串方法

C++试图使构造函数失败