2011-02-13 37 views
25

说我有以下三个常量:找到3号在Java中最大不同数据类型

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); 

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

回答

64

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

+2

+1我正要提交了相同的答案。 – 2011-02-13 03:20:31

+0

当涉及n个值时,必须有更好的方法。 – mlissner 2013-04-16 23:27:36

+2

@mlissner是的,使用一个循环和一个变量`max`,检查每个变量是否大于`max`,如果是的话:将`max`设置为该变量。假设你的n值当然是在一个数组中。 – 2013-09-20 17:12:11

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; 
    } 
} 

这将需要任意数量的混合数值参数(整数,double和float),但返回的值是一个对象,所以你必须把它转换为整型,双精度或浮点型。

由于没有“MY_DOUBLE2”之类的东西,它可能也会引发错误。

+0

现在我只是一个小白自己,如果任何人都可以帮助我清理它,那将是不胜感激...... – 2013-06-04 21:34:52

6

不使用第三方库,调用相同的方法不止一次或创建一个数组更多,你可以找到像这样

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); 
} 
+0

我相信问题是关于使用`Math.max`不重新创建`max`函数。 – 2015-03-17 21:15:19

2

我有一个很简单的想法:

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

当然,如果你有1000 numbers,这是无法使用的,但如果你有34号码,它的方便和快捷。

问候, 诺伯特

+0

我敢肯定,问题是关于最大数量...不是最小数量;) – Edd 2016-06-16 09:22:36

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

,如果你想要做一个简单的,它会是这样

// 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™ How To Program (Early Objects), Tenth Edition

1

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

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

您可以使用此:

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

,或者创建一个功能

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