这是我正在尝试解决的问题的代码
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 []无法转换为&gt; Integer [] output = totalchocolates(ip1);
答案 0 :(得分:1)
错误清楚地表明您提供int[]
作为输入参数,而您的函数期望Integer[]
数组。将输入数组更改为键入Integer[]
Integer[] ip1 = new Integer[ip1_size];
答案 1 :(得分:0)
在Java中,Integer和int引用不同的类型 - Integer在对象类型中包装int,并提供了几种实用方法。结果,这些阵列是不兼容的。你需要两者都是int []或Integer []