简体   繁体   English

类型不匹配:Java 代码中无法从 int 转换为 int[] 错误

[英]Type mismatch: cannot convert from int to int[] error in Java code

I'm receiving an error on the following Java code:我在以下 Java 代码中收到错误消息:

public int[] maxEnd3(int[] nums) {
  int larger = Math.min(nums[0],nums[2]);
  return larger;
}
 Error: int larger [] = Math.min(nums[0],nums[2]); ^^^^^^ Type mismatch: cannot convert from int to int[]

Why am I not able to calculate the minimum of 2 array values?为什么我无法计算 2 个数组值中的最小值?

if you want to return the larger in an array then change the return of the method maxEnd3 to int and not array of int..如果要返回数组中较大的值,则将方法maxEnd3的返回值更改为 int 而不是 int 数组。

public int maxEnd3(int[] nums) {
  int larger = Math.min(nums[0],nums[2]);
  return larger;
}

Your return type is int[], so it is expecting int[] but you are returning a "large" which is an int.你的返回类型是 int[],所以它期待 int[] 但你返回一个“大”,它是一个 int。 Try changing your return type to int or make "large" int[].尝试将您的返回类型更改为 int 或使“大”int[]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM