【问题标题】:error: incompatible types: int[] cannot be converted to Integer[]错误:不兼容的类型:int[] 无法转换为 Integer[]
【发布时间】:2018-05-28 02:40:32
【问题描述】:

这是我要解决的问题的代码

public static int totalchocolates(Integer[] input1) {

    int countEaten = 0;
    Arrays.sort(input1, Collections.reverseOrder());

    for (int i = input1.length - 1; i > -1; i--) {
        countEaten = (int)(countEaten + Math.ceil(input1[i].doubleValue() / 2));
        if (i > 1 && (input1[i - 1] + input1[i] / 2) > 1000) {
            i = i - 1;
        }
    }
    return countEaten;
}

主要功能是

public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(System.in);
    int output = 0;
    int ip1_size = 0;
    ip1_size = Integer.parseInt(in.nextLine().trim());
    int[] ip1 = new int[ip1_size];
    int ip1_item;
    for (int ip1_i = 0; ip1_i < ip1_size; ip1_i++) {
        ip1_item = Integer.parseInt( in .nextLine().trim());
        ip1[ip1_i] = ip1_item;
    }
    output = totalchocolates(ip1);
    System.out.println(String.valueOf(output));
}

我收到以下错误,

CandidateCode.java:36:错误:不兼容的类型:int[] 无法转换为 >Integer[] 输出 = 总巧克力(ip1);

【问题讨论】:

  • 这个错误很容易解释。你有什么问题?
  • ip1的声明改为Integer[] ip1 = new Integer[ip1_size];

标签: java arrays


【解决方案1】:

错误清楚地表明您提供 int[] 作为输入参数,而您的函数需要 Integer[] 数组。最好把输入数组改成Integer[]

Integer[] ip1 = new Integer[ip1_size];

【讨论】:

  • 请不要在答案中使用文字朗读。需要完整、正确的英语。我修复了它,并纠正了一些格式问题。 +1
  • 谢谢吉姆。我刚刚开始回答有关堆栈溢出的问题。今天将在回答问题的同时进行格式化和其他工作......
【解决方案2】:

在 Java 中,Integer 和 int 指代不同的类型 - Integer 将 int 包装在一个对象类型中,并提供了多种实用方法。因此,这些数组是不兼容的。您需要两者都是 int[] 或 Integer[]

【讨论】:

    猜你喜欢
    • 2018-10-28
    • 2017-03-27
    • 2017-10-03
    • 1970-01-01
    • 2016-09-05
    • 2023-01-18
    • 2019-08-22
    • 2017-03-29
    • 1970-01-01
    相关资源
    最近更新 更多