Math.min错误 - 练习错误

时间:2018-03-17 00:46:11

标签: 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编程语言的新手,我对如何解决这个错误非常困惑。

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

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

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

这并不像函数式编程那样华丽,但它仍然与Java7兼容,因为在某些项目中由于某种原因仍然无法使用Java8。

或者您可以使用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...-

答案 1 :(得分:0)

Math.min(int, int)不会带三个(或更多)参数。它只需要两个。变化

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

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

答案 2 :(得分:0)

Math.min仅限于两个参数,您可以将一个Math.min的结果传递到另一个Math.min来电,就像Elliott在答案中所示,或者您可以这样做:< / p>

int min = IntStream.of(species, Q13, shadow).min().getAsInt();
相关问题