2011-02-13 51 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)); 
} 
相關問題