0

Im trying to run a for loop that prints each users score in an array list. I don't know how to output who has the greatest/highest score however. Can someone please help me?

for (int i = 0; i < playersList.size(); i++) {

        System.out.println("Player " + (i + 1) + ", "
                + playersList.get(i).getName() + " Score: " + playersList.get(i).getTotalScore());
        playersList.get(i).getFrameScores();


        System.out.println("**************");

    }
0

3 Answers 3

4

First, find the max score using one iteration over your elements in list. Then, use another iteration to seek whose players have the max score and print them.

Here's a pseudocode for the above algorithm:

int maxScore <- Integer.MIN_VALUE
for each player in playerList
    if maxScore < player->score then
        maxScore <- player->score
    end if
end for
//now you have the max score
for each player in playerList
    if player-> score == maxScore then
        show player info
    end if
end for
Sign up to request clarification or add additional context in comments.

3 Comments

@user2864740 what do you mean by JOBOL, Java + Cobol or something similar?
Yeah :-) However, I do like this answer (as much as I will allow myself), because it also addresses the case where multiple people can have the same max score.
@user2864740 well, there's no need to get too complex for easy tasks :). Besides, since it is pseudocode, the implementation may use a Comparator if needed for the score comparison.
0

Try this:

double maxScore = playersList.get(0).getTotalScore();
ArrayList<String> maxNames = new ArrayList();
maxNames.add(playersList.get(0).getName());
for (int i = 1; i < playersList.size(); i++) {
    if(playersList.get(i).getTotalScore() > maxScore) {
        maxScore = playersList.get(i).getTotalScore();
        maxNames.clear();
    }
    if(playersList.get(i).getTotalScore() >= maxScore ) {
        maxNames.add(playersList.get(i).getName());
    }
}
StringBuilder names = "";
for(int i=0; i<maxNames.size(); i++) {
    names.append(maxNames.get(i));
    if(i != maxNames.size() - 1) {
        names.append(", ");
    }
}
System.out.println("The player" + (maxNames.size() > 1 ? "s" : "") + " with the highest score " + (maxNames.size() > 1 ? "are" : "is") + " " + names + ", with a score of " + maxScore + ".");

3 Comments

What if there are several players with maximum score?
@LuiggiMendoza It will output the first one to get the highest score. I will fix that.
Fixed. It will now output all players with the maximum score.
-1

Assign the first value as max and compare every coming values. If coming value is large set it as max and compare it with remaining.

For ex

var max =0;

for(i=o;i<5;i++) {
  if(i==0){ var max = a[i]; }

   if(max < a[i]) {
      max= a[i];
   }
}

alert(max);

2 Comments

Isn't this JavaScript?
Yes this is javascript, but you can adopt same logic for java to achieve the outcome.

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.