Nafroman
Nafroman

Reputation: 13

java, getting the highest value from multiple arrays

int[] player1,player2,player3,player4;
int[] player1 =new int[12];
int[] player2 =new int[12];
int[] player3 =new int[12];
int[] player4 =new int[12];

Each player has 13 random numbers inside. Is there a way to compare player1[i] with players2,3,4[i] to find the highest value? The player with the highest value will win that round.

int score = 0;

for (int i = 0; i < player1[i]; i++){
    if (player[i] > score)
    score = player[i];
}

This is the code I've made, but it can only compare one player with a score.

Upvotes: 1

Views: 344

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533500

I would write it like this

public static int max(int... nums) {
    int max = Integer.MIN_VALUE;
    for(int i: nums) if(max < i) max = i;
    return max;
}

int[] player1,player2,player3,player4;
....
int allMax = max(max(player1), max(player2), max(player3), max(player4));

i.e. the max of all, is the max of the individual maximums.

Upvotes: 1

Related Questions