Salutations, all! I'm taking my first Java class in college and working on a personal project on the side so this is not an assignment and not urgent in any way. I've been really impressed with reading the posts on this forum, particular the various guides people have posted. Also a special nod to KevinWorkman for helping me out last time, thanks again!
I tried searching the forums for a similar thread but apparently "max" and "value" are too common to search for and "max value in an array" seemed to return many "array" threads but not any relevant ones. I apologize in advance if this is a redundant problem, all I've been able to find on the subject is how to find a single max value in the array.
The Program
I have an array of Player objects (created at the start of the program, can be any valid int in length) which have an int Number and int Score. I need to find the highest score in the array and record that Player's Number and Score. Here's my code right now:
// Player with highest score wins for (counter = 0; counter < players.length; counter++) { System.out.println("Player" + players[counter].getNumber() + " 's final score is " + players[counter].getScore() ); if (players[counter].getScore() > winnerScore){ winnerScore = players[counter].getScore(); winner = players[counter].getNumber(); } else{ } } System.out.println( "Player" + winner + " wins with " + winnerScore + " points!" );
The Problem
This seems to work how I want it to except it has no way of accounting for ties, i.e. when more than one player has the same score. Because the for loop goes through the array entry by entry the first player with the high score will always come out as the winner.
Possible Solution
I believe any solution to this problem requires creating a new array of Player objects for winning players equal to the original Player array's length, since it's possible for all players to be tied. I could use an if statement to move player objects to this new array (if their score was >= the current highest score) but since the for loop starts at the beginning of the array it would always get Player1 and would get other players that didn't necessarily have the highest score-just the highest to that point.
If I could create a winning array and move Players objects with a Score => winnerScore to it while also removing any Player objects with a Score < the Score of the new Player object being added I believe that would work. In order to do that I would need to have another for loop go through the winner's array and find the max score there, then remove all Player objects with a lower Score? That seems like it's overly redundant to me.
Another Possible Solution
Sorry if these seem a bit stream-of-conscious but I'm just thinking of them as I post this message. If I had the code above find the winnerScore, I could have another for loop go through the array and assign all players with a Score equal to winnerScore to a new winner's array, then print every Player object's number and score in that array? Now that I've thought of it that seems like the right thing to do, but I'm wondering if it's possible to do it in a single loop.
Unfortunately I do not have a Java editor on the computer I am at now or else I would try the latter method above which I do think will work if I'm thinking of it right. However I'd really like to keep the coding as simple as possible and I'm still unable to think of a single-loop method. Again this is just for fun so I don't mean to be a burden, but I'm excited because I got such a quick and helpful response last time.
In any case thanks again to all the people who help out on this forum, it's great to know there's a place like this if something seriously important ever did come up.