Find the max of 3 numbers in Java with different data types

前端 未结 12 1888
情深已故
情深已故 2020-12-03 00:33

Say I have the following three constants:

final static int MY_INT1 = 25;
final static int MY_INT2 = -10;
final static double MY_DOUBLE1 = 15.5;
相关标签:
12条回答
  • 2020-12-03 01:17

    Java 8 way. Works for multiple parameters:

    Stream.of(first, second, third).max(Integer::compareTo).get()
    
    0 讨论(0)
  • 2020-12-03 01:18

    You can do like this:

    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 讨论(0)
  • 2020-12-03 01:19

    if you want to do a simple, it will be like this

    // 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
    

    and the output will be something like this

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

    References Java™ How To Program (Early Objects), Tenth Edition

    0 讨论(0)
  • 2020-12-03 01:21

    I have a very simple idea:

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

    Of course, if you have 1000 numbers, it's unusable, but if you have 3 or 4 numbers, its easy and fast.

    Regards, Norbert

    0 讨论(0)
  • 2020-12-03 01:25

    Without using third party libraries, calling the same method more than once or creating an array, you can find the maximum of an arbitrary number of doubles like so

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

    In your example, max could be used like this

    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 讨论(0)
  • 2020-12-03 01:29

    Math.max only takes two arguments. If you want the maximum of three, use Math.max(MY_INT1, Math.max(MY_INT2, MY_DOUBLE2)).

    0 讨论(0)
提交回复
热议问题