Type Mismatch: 无法将 double 转换为 double[]

huangapple go评论112阅读模式
英文:

Type Mismatch: Can't convert double into double[]

问题

当我运行这段Java代码时,出现了错误。我是Java的新手。有人可以帮忙吗?

  1. package raza.project.arrays;
  2. import java.util.*;
  3. public class arraysLearning {
  4. public static void main(String[] args) {
  5. int [] numbers = { 25,36,38,63,89,52,74};
  6. double[] med = median(numbers);
  7. }
  8. // 返回一个数组中的中位数值,不改变参数数组
  9. public static double median(int[] numbers) {
  10. int[] tmp = Arrays.copyOf(numbers, numbers.length);
  11. Arrays.sort(tmp);
  12. int mid = tmp.length/2; // 注意:整数除法
  13. if (tmp.length%2 == 0) { // 偶数长度?
  14. return (tmp[mid-1]+tmp[mid])/2.0; // 浮点数除法
  15. }
  16. else {
  17. return tmp[mid];
  18. }
  19. }
  20. }

Java编译器是最新的JDK14

英文:

When i am running this java code i got error. I am new to java. Can anyone help.

  1. package raza.project.arrays;
  2. import java.util.*;
  3. public class arraysLearning {
  4. public static void main(String[] args) {
  5. int [] numbers = { 25,36,38,63,89,52,74};
  6. double[] med = median(numbers);
  7. }
  8. //Return the median value of an array of numbers
  9. //without changing the parameter array
  10. public static double median(int[] numbers) {
  11. int[] tmp = Arrays.copyOf(numbers, numbers.length);
  12. Arrays.sort(tmp);
  13. int mid = tmp.length/2; // Note: int division
  14. if (tmp.length%2 == 0) { // even length?
  15. return (tmp[mid-1]+tmp[mid])/2.0; //float division
  16. }
  17. else {
  18. return tmp[mid];
  19. }
  20. }
  21. }

java compiler is latest jdk14

答案1

得分: 2

这个方法:public static double median(int[] numbers) 返回一个 double 类型的对象。

当你想调用一个方法并将结果存储在一个变量中时,你需要确保变量的类型与方法返回的类型相同。

你需要更改这行代码:

  1. double[] med = median(numbers);

  1. 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:

  1. double[] med = median(numbers);

With

  1. 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.

  1. public static void main(String[] args) {
  2. int[] numbers = {25, 36, 38, 63, 89, 52, 74};
  3. double med = median(numbers); // remove [] on this line.
  4. }
英文:

Your median() method is supposed to return a "double" value and not an array of double values.

  1. public static void main(String[] args) {
  2. int[] numbers = {25,36,38,63,89,52,74};
  3. double med = median(numbers); // remove [] on this line.
  4. }

答案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 {

  1. public static void main(String[] args) {
  2. int [] numbers = { 25,36,38,63,89,52,74};
  3. double med = median(numbers);
  4. }
  5. //Return the median value of an array of numbers
  6. //without changing the parameter array
  7. public static double median(int[] numbers) {
  8. int[] tmp = Arrays.copyOf(numbers, numbers.length);
  9. Arrays.sort(tmp);
  10. int mid = tmp.length/2; // Note: int division
  11. if (tmp.length%2 == 0) { // even length?
  12. return (tmp[mid-1]+tmp[mid])/2.0; //float division
  13. }
  14. else {
  15. return tmp[mid];
  16. }
  17. }

}

huangapple
  • 本文由 发表于 2020年7月28日 16:30:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63130032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定