数学分钟错误 - 练习错误

  • 本文关键字:错误 练习 分钟 java
  • 更新时间 :
  • 英文 :


当我输入答案时,我得到了这个:

The given method does not accept the given parameter types.
no suitable method found for min(int,int,int)
    Math.min(species, Q13, shadow);
        ^
    method Math.min(double,double) is not applicable
      (actual and formal argument lists differ in length)
    method Math.min(float,float) is not applicable
      (actual and formal argument lists differ in length)
    method Math.min(int,int) is not applicable
      (actual and formal argument lists differ in length)
    method Math.min(long,long) is not applicable
      (actual and formal argument lists differ in length)

知道我如何解决上述错误吗?我是这个网站和Java编程语言的新手,我对如何修复此错误感到非常困惑。

感谢您的帮助!

或者,您可以创建自己的 min 函数,如下所示:

public static int min(int... params) {
    int min = Integer.MAX_VALUE;
    for (int param : params) {
        if (param < min)
            min = param;
    }
    return min;
}

这不像函数式编程那样花哨,但对于那些由于某种原因仍然不能在某些项目中使用 Java8 的人来说,它仍然与 Java7 兼容。

或者你可以只使用Apache Commons ObjectUtils.min泛型函数:

@SafeVarargs
public static <T extends Comparable<? super T>> T min(T... values)

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/ObjectUtils.html#min-T...-

Math.min(int, int)不接受三个(或更多(参数。只需要两个。改变

int variable = Math.min(species, Q13, shadow);

int variable = Math.min(species, Math.min(Q13, shadow));
Math.min仅限于

两个参数,您可以将一个Math.min的结果传递到另一个Math.min调用中,就像 Elliott 在他的答案中所示,或者您可以这样做:

int min = IntStream.of(species, Q13, shadow).min().getAsInt();

最新更新