0

I was wondering if there was a method in java that takes your variables as parameters and returns the max integer value, or if i should be writing my own method for that. In my program, i have 4 different integers that change everytime the method is called and are all stored in different variables (rightDiag, leftDiag, hor and vert) for a tic tac toe game, and at the end I have to determine which has the highest value. Is there any methods that can take these variables as parameter and return the highest integer out of all four? (or something along those lines) or should i be writing my own "max" method?

Thanks everyone!

6
  • 2
    Math.max() note that there are many Math.max's
    – Justin
    Nov 1, 2013 at 23:57
  • you mean like max(1,2)?
    – A4L
    Nov 1, 2013 at 23:57
  • @A4L try to use the new Java 7 Documentation.
    – Justin
    Nov 1, 2013 at 23:58
  • 1
    This is a trivial function. You should write it yourself.
    – user987339
    Nov 2, 2013 at 0:00
  • @Quincunx, fixed, although I don't think it matters a lot in this particular case ;)
    – A4L
    Nov 2, 2013 at 0:04

5 Answers 5

3

Math.max() works for 2 parameters. It's implemented like this:

int max(int a, int b) {
  return a > b ? a : b;
}

Hope that's enough of an inspiration for your own implementation.

3

If you're ready to use Java 8:

int max = IntStream.of(a, b, c, d).max().getAsInt();

But there's nothing like this in Java 7.

3
  • Now I gotta go look this up.
    – ajb
    Nov 2, 2013 at 0:10
  • Should that be IntStream?
    – ajb
    Nov 2, 2013 at 0:14
  • handy! OptionalInt seems odd.
    – sschrass
    Sep 20, 2017 at 14:35
1

Math.max only takes two parameters, but you can use it repeatedly:

Math.max(rightDiag, Math.max(leftDiag, Math.max(hor, vert)))

Or if you want to be fancy you can write a method that takes a variable number of parameters:

static int max(int... parameters) {
    // left as an exercise
}

and the code will treat parameters as an int[] array, and you can go through the array and find the largest one.

0

There a lot of ways in which you can implement such a method. If you don't want to use a lot of if statements to determine which value is the highest, you can store all the values in an array, loop through the array and keep a pointer to the highest value in the array, then return that value at the end.

0
static int max(int... args) 
{ //This is one implementation, but for practice, try doing one with a foreach loop instead.
    int result=args[0];
    for(int i=1;i<args.length;i++)
    {
        result=Math.max(result,args[i]);
    }
    return result;
}

Then you can call

int maxValue = max(rightDiag, leftDiag, hor, vert);
2
  • 1
    yeah, let's do his homework for him, why not. I hope he gets hired into where you work.
    – iluxa
    Nov 2, 2013 at 0:05
  • @iluxa: Is this homework? Regardless, this is such a trivial method that if he can't write it on his own, he's going to have plenty of problems with the rest of the assignment, so I might as well provide a standard reference for others. You make a valid point though. Nov 2, 2013 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.