Java 如何创建获取 4 个数字并返回最大数字的 max 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27293080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:07:02  来源:igfitidea点击:

How to create max method that gets 4 numbers and returns the maximum number?

javaif-statementnumbersreturnmax

提问by YoAv

I'm trying to build a method that would get 4 numbers and returns the maximum number of them.

我正在尝试构建一个可以获取 4 个数字并返回它们的最大数量的方法。

I tried to write this code that gets 4 numbers but this not working:

我试图编写这段获得 4 个数字的代码,但这不起作用:

Input and output:

输入输出:

double a = Math.max(10, 5, 4, 3);
    System.out.println(a);

public static int max(int a, int b, int c, int d) {
    if (a > b && a > c && a > d)
        return a;
    if (b > a && b > c && b > d)
        return b;
    if (c > a && c > b && c > d)
        return c;
    if (d > b && d > c && d > a)
        return d;
}

采纳答案by Patrick Hofman

I would simplify this by introducing a variable max:

我会通过引入一个变量来简化这个max

public static int max(int a, int b, int c, int d) {

    int max = a;

    if (b > max)
        max = b;
    if (c > max)
        max = c;
    if (d > max)
        max = d;

     return max;
}

You could also use Math.max, as suggestedby fast snail, but since this seems to be homework, I would prefer the algorithmic solution.

你也可以使用Math.max,如建议通过快速的蜗牛,但因为这似乎是家庭作业,我宁愿算法的解决方案。

Math.max(Math.max(a,b),Math.max(c,d))

回答by SMA

Try Math.maxlike below:

尝试Math.max如下:

return Math.max(Math.max(a, b), Math.max(c, d));

回答by PARITOSH THAPLIYAL

if (c > a && c > b && c > d)
    return d;

here you are returning d instead of c.

在这里,您返回的是 d 而不是 c。

回答by Erich Kitzmueller

One more way to do it...

另一种方法来做到这一点......

public static int max(int a, int b, int c, int d) {
    if (a > b && a > c && a > d)
        return a;
    if (b > c && b > d)
        return b;
    if (c > d)
        return c;
    return d;
}

回答by BarrySW19

You could always use a method like this which will work as you wanted for any number of integers:

你总是可以使用这样的方法,它可以为任意数量的整数工作:

public static Integer max(Integer... vals) {
    return new TreeSet<>(Arrays.asList(vals)).last();
}

Call, for example, as:

例如,调用如下:

System.out.println(max(10, 5, 17, 4, 3));

回答by maimArt

public static Integer max(Integer... values)
{
     Integer maxValue = null
     for(Integer value : values)
         if(maxValue == null || maxValue < value)
              maxValue = value;
     return maxValue;
}

回答by Ar maj

public static int max(int a, int b, int c, int d){
        return (a>b && a>c && a>d? a: b>c && b>d? b: c>d? c:d);
    }

回答by coja

public static int max(int a, int b, int c, int d) {

   int tmp1 = a > b ? a : b;
   int tmp2 = c > d ? c : d;

   return tmp1 > tmp2 ? tmp1 : tmp2;
}

回答by shifu

public static int max(Integer... vals) {
    return Collections.max(Arrays.asList(vals));
}

回答by Karthik Bose

private int max(int... p)
{
    int max = 0;
    for (int i : p) {
        max = i > max ? i : max; 
    }
    return max;

}