Answer to Question #48546 in Java | JSP | JSF for Justin

Question #48546
How do I write a program that prompts the user to enter the number of players and each player’s name and number of points scored, then display the name of the player with the highest score and the high score?
Expert's answer
Firstly you must create some class for player like this:
public class Player {
private String name;
private int score;

public Player(String name, int score) { name;
this.score = score;
//if you need a setters
//you can add it
public String getName() {
return name;
//this score we use when we'll search max value
public int getScore() {
return score;

In main method create Player[] array = newPlayer[number];//or playerArray, players,...
when number is entered by user:
Scanner scanner = new Scanner(;
//here you invite user to enter players number
//here you creates array
for (int i = 0; i < number; i++) {
System.out.print("Enter player name:");
String name = scanner.nextString();
System.out.println("Enter player score:");
int score = scanner.nextInt();
array[i] = new Player(name, score);

Then create static method(or it can't be called from main)like this:
private static Player getMaxScorePlayer(Player[] players) {
//here we search max score player
Player maxScorePlayer = players[0]; //here
for (int i = 1; i < players.length; i++) {//from 1 because players[0] we already have
if (players[i].getScore() >maxScorePlayer.getScore()) {
maxScorePlayer =players[i];
return maxScorePlayer;

and in main you call this method
Player maxPlayer = getMaxScorePlayer(array); //array - ourplayers array
System.out.println("Max score player: " +maxPlayer.getName() + "(" + maxPlayer.getScore() + ")");

That's all

You can also add to Player class toString() method and thenin last line:
System.out.println("Max score player: " +maxPlayer);

Don't forget about @Override annotation:
class Player {
public String toString() {
return "Your formatstring";

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be first!

Leave a comment

Ask Your question

New on Blog