英文:
Type Mismatch: Can't convert double into double[]
问题
当我运行这段Java代码时,出现了错误。我是Java的新手。有人可以帮忙吗?
package raza.project.arrays;
import java.util.*;
public class arraysLearning {
public static void main(String[] args) {
int [] numbers = { 25,36,38,63,89,52,74};
double[] med = median(numbers);
}
// 返回一个数组中的中位数值,不改变参数数组
public static double median(int[] numbers) {
int[] tmp = Arrays.copyOf(numbers, numbers.length);
Arrays.sort(tmp);
int mid = tmp.length/2; // 注意:整数除法
if (tmp.length%2 == 0) { // 偶数长度?
return (tmp[mid-1]+tmp[mid])/2.0; // 浮点数除法
}
else {
return tmp[mid];
}
}
}
Java编译器是最新的JDK14
英文:
When i am running this java code i got error. I am new to java. Can anyone help.
package raza.project.arrays;
import java.util.*;
public class arraysLearning {
public static void main(String[] args) {
int [] numbers = { 25,36,38,63,89,52,74};
double[] med = median(numbers);
}
//Return the median value of an array of numbers
//without changing the parameter array
public static double median(int[] numbers) {
int[] tmp = Arrays.copyOf(numbers, numbers.length);
Arrays.sort(tmp);
int mid = tmp.length/2; // Note: int division
if (tmp.length%2 == 0) { // even length?
return (tmp[mid-1]+tmp[mid])/2.0; //float division
}
else {
return tmp[mid];
}
}
}
java compiler is latest jdk14
答案1
得分: 2
这个方法:public static double median(int[] numbers)
返回一个 double
类型的对象。
当你想调用一个方法并将结果存储在一个变量中时,你需要确保变量的类型与方法返回的类型相同。
你需要更改这行代码:
double[] med = median(numbers);
为
double med = median(numbers);
在这里,变量的类型是 double
,而方法返回一个 double
。两者类型匹配,编译器满意,你的代码可以编译。但在你的情况下,类型是 double[]
,而方法返回一个 double
对象... 没有匹配,所以你的代码无法编译。
英文:
This method: public static double median(int[] numbers)
returns an object of type double
.
When you want to call a method a get the result in a variable, you need to make sure the variable is of the same type as the type returned by the method.
You need to change this line:
double[] med = median(numbers);
With
double med = median(numbers);
Here, the variable is of type double
and the method returns a double
. Both types matches, the compiler is happy and your code compiles. But in your case, the type is double[]
and the method returns a double
object... There is no match, so your code doesn't compile
答案2
得分: 0
Your median() method is supposed to return a "double" value and not an array of double values.
public static void main(String[] args) {
int[] numbers = {25, 36, 38, 63, 89, 52, 74};
double med = median(numbers); // remove [] on this line.
}
英文:
Your median() method is supposed to return a "double" value and not an array of double values.
public static void main(String[] args) {
int[] numbers = {25,36,38,63,89,52,74};
double med = median(numbers); // remove [] on this line.
}
答案3
得分: 0
You are returning a double value but you are returning it to a double array, there is type mismatch.
你返回一个 double 值,但你将它返回给一个 double 数组,类型不匹配。
英文:
You are returning a double value but you are returning it to a double array, there is type mismatch.
import java.util.*;
public class Test {
public static void main(String[] args) {
int [] numbers = { 25,36,38,63,89,52,74};
double med = median(numbers);
}
//Return the median value of an array of numbers
//without changing the parameter array
public static double median(int[] numbers) {
int[] tmp = Arrays.copyOf(numbers, numbers.length);
Arrays.sort(tmp);
int mid = tmp.length/2; // Note: int division
if (tmp.length%2 == 0) { // even length?
return (tmp[mid-1]+tmp[mid])/2.0; //float division
}
else {
return tmp[mid];
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论