在Java中查找不同数据类型的3个数字中的最大值

问题描述 投票:0回答:12

假设我有以下三个常数:

final static int MY_INT1 = 25;
final static int MY_INT2 = -10;
final static double MY_DOUBLE1 = 15.5;

我想获取其中的三个值并使用

Math.max()
找到三个值中的最大值,但是如果我传入两个以上的值,则会出现错误。例如:

// this gives me an error
double maxOfNums = Math.max(MY_INT1, MY_INT2, MY_DOUBLE2);

请让我知道我做错了什么。

java math max
12个回答
107
投票

Math.max
仅接受两个参数。如果您想要最多三个,请使用
Math.max(MY_INT1, Math.max(MY_INT2, MY_DOUBLE2))


28
投票

你可以使用这个:

 Collections.max(Arrays.asList(1,2,3,4));

或创建一个函数

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

28
投票

如果可能,请使用 Apache Commons Lang 中的 NumberUtils - 那里有很多很棒的实用程序。

https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/math/NumberUtils.html#max(int[])

NumberUtils.max(int[])

13
投票

Math.max
仅接受两个参数,不多也不少。

已发布答案的另一个不同解决方案是使用

DoubleStream.of
:

double max = DoubleStream.of(firstValue, secondValue, thirdValue)
                         .max()
                         .getAsDouble();

10
投票

Java 8 方式。适用于多个参数:

Stream.of(first, second, third).max(Integer::compareTo).get()

9
投票

无需使用第三方库,多次调用相同的方法或创建数组,您就可以像这样找到任意数量的双精度数的最大值

public static double max(double... n) {
    int i = 0;
    double max = n[i];

    while (++i < n.length)
        if (n[i] > max)
            max = n[i];

    return max;
}

在您的示例中,

max
可以像这样使用

final static int MY_INT1 = 25;
final static int MY_INT2 = -10;
final static double MY_DOUBLE1 = 15.5;

public static void main(String[] args) {
    double maxOfNums = max(MY_INT1, MY_INT2, MY_DOUBLE1);
}

4
投票

我有一个非常简单的想法:

int smallest = Math.min(a, Math.min(b, Math.min(c, d)));

当然,如果你有1000个号码,那就没法用了,但如果你有3、4个,那就简单又快捷了。


1
投票

就像之前提到的,Math.max() 仅接受两个参数。它与您当前的语法不完全兼容,但您可以尝试 Collections.max()。

如果您不喜欢这样,您可以随时为其创建自己的方法...

public class test {
    final static int MY_INT1 = 25;
    final static int MY_INT2 = -10;
    final static double MY_DOUBLE1 = 15.5;

    public static void main(String args[]) {
        double maxOfNums = multiMax(MY_INT1, MY_INT2, MY_DOUBLE1);
    }

    public static Object multiMax(Object... values) {
        Object returnValue = null;
        for (Object value : values)
            returnValue = (returnValue != null) ? ((((value instanceof Integer) ? (Integer) value
                    : (value instanceof Double) ? (Double) value
                            : (Float) value) > ((returnValue instanceof Integer) ? (Integer) returnValue
                    : (returnValue instanceof Double) ? (Double) returnValue
                            : (Float) returnValue)) ? value : returnValue)
                    : value;
        return returnValue;
    }
}

这将采用任意数量的混合数字参数(Integer、Double 和 Float),但返回值是一个对象,因此您必须将其转换为 Integer、Double 或 Float。

它也可能会抛出错误,因为不存在“MY_DOUBLE2”这样的东西。


1
投票
int first = 3;  
int mid = 4; 
int last = 6;

//checks for the largest number using the Math.max(a,b) method
//for the second argument (b) you just use the same method to check which  //value is greater between the second and the third
int largest = Math.max(first, Math.max(last, mid));

1
投票

你可以这样做:

public static void main(String[] args) {

    int x=2 , y=7, z=14;
    int max1= Math.max(x,y);

    System.out.println("Max value is: "+ Math.max(max1, z)); 
}  

0
投票

如果想做简单的话就这样

// Fig. 6.3: MaximumFinder.java
// Programmer-declared method maximum with three double parameters.
import java.util.Scanner;

public class MaximumFinder
{
  // obtain three floating-point values and locate the maximum value
  public static void main(String[] args)
  {
    // create Scanner for input from command window
    Scanner input = new Scanner(System.in);

    // prompt for and input three floating-point values
    System.out.print(
      "Enter three floating-point values separated by spaces: ");
    double number1 = input.nextDouble(); // read first double
    double number2 = input.nextDouble(); // read second double
    double number3 = input.nextDouble(); // read third double

    // determine the maximum value
    double result = maximum(number1, number2, number3);

    // display maximum value
    System.out.println("Maximum is: " + result);
  }

  // returns the maximum of its three double parameters          
  public static double maximum(double x, double y, double z)     
  {                                                              
    double maximumValue = x; // assume x is the largest to start

    // determine whether y is greater than maximumValue         
    if (y > maximumValue)                                       
      maximumValue = y;                                        

    // determine whether z is greater than maximumValue         
    if (z > maximumValue)                                       
      maximumValue = z;                                        

    return maximumValue;                                        
  }                                                              
} // end class MaximumFinder

输出将是这样的

Enter three floating-point values separated by spaces: 9.35 2.74 5.1
Maximum is: 9.35

参考资料 Java™ 如何编程(早期对象),第十版


0
投票

没有方法的简单方法

int x = 1, y = 2, z = 3;

int biggest = x;
if (y > biggest) {
    biggest = y;
}
if (z > biggest) {
    biggest = z;
}
System.out.println(biggest);
//    System.out.println(Math.max(Math.max(x,y),z));
© www.soinside.com 2019 - 2024. All rights reserved.