Como se mencionó antes, Math.max() sólo toma dos argumentos. No es exactamente compatible con su sintaxis actual, pero podría intentar Collections.max().
Si no te gusta que siempre puede crear su propio método para ello ...
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;
}
}
Esto tomar cualquier número de argumentos numéricos mixtos (Integer, Double y flotar), pero el valor de retorno es un Objeto, por lo que tendrías que convertirlo en Integer, Double o Float.
También podría estar arrojando un error ya que no existe tal cosa como "MY_DOUBLE2".
+1 yo estaba a punto de presentar la misma respuesta. –
Debe haber una mejor manera cuando hay n valores involucrados. – mlissner
@mlissner Sí, use un bucle y una variable 'max', compruebe cada variable, independientemente de si son mayores que 'max', de ser así: establezca 'max' en esa variable. Suponiendo que sus valores n están en una matriz, por supuesto. –