1
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.

8
  • So, what is the actual question here ? Did you write any code towards the solution ? Commented Feb 25, 2014 at 20:03
  • 'code' int score = 0; for (int i = 0; i < player1[i]; i++){ if (player[i] > score) score = player[i]; } this is the code ive made but it can only compare one player with a score Commented Feb 25, 2014 at 20:07
  • please, edit the question as writing code in comments is totally unreadable Commented Feb 25, 2014 at 20:08
  • 1
    Each player can't have 13 random numbers, since the array length is 12. And your loop is wrong: i < player1[i] doesn't make sense. You want i to go from 0 to the length of the array (not included) Commented Feb 25, 2014 at 20:13
  • but position 0 would be the first value? Commented Feb 25, 2014 at 20:17

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@JBNizet Well spotted, much cleaner.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.