Solved Get biggest int

Discussion in 'Plugin Development' started by iliasdewachter, Apr 29, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    iliasdewachter

    I got 3 integers:

    Code:
    int vote1 = votes.get(0);
    int vote2 = votes.get(1);
    int vote3 = votes.get(2);
    So, how do I get the biggest integer? (I've already tried Math.max(int,int) but it only has 2 arguments...)
     
  2. Offline

    Rocoty

    if you have a fixed number of integers, you could easily get the biggest by doing
    Code:java
    1. Math.max(Math.max(vote1, vote2), vote2);


    Although, if you don't know how many integers you will have to get the max value of, store all the values into an array and do this
    Code:java
    1. int max = 0;
    2. for (int vote : votes) {
    3. if (vote > max) {
    4. max = vote;
    5. }
    6. }

    The variable called max is now the max value.
     
  3. Offline

    iliasdewachter

    Omg, I feel stupid ;)

    Thanks for your help!
     
  4. Offline

    Rocoty

    iliasdewachter
    And here is a simple method I made which will take as many arguments as you could possibly wish for:
    Code:java
    1. private double max(double... args) {
    2. double max = 0.0;
    3. for (double d : args) {
    4. if (d > max) {
    5. max = d;
    6. }
    7. }
    8. return max;
    9. }


    Just in case you wanted one.
     
  5. Offline

    teunie75

Thread Status:
Not open for further replies.

Share This Page