My code wont print results then im trying to print the tallest player name and a
My code wont print results then im trying to print the tallest player name and age with their height and inchest my code: import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Project1 {
private static Player tallestPlayer;
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
List players = new ArrayList<>();
System.out.println(“Enter the number of players:”);
int numPlayers = scanner.nextInt();
for (int i = 0; i < numPlayers; i++) {
System.out.println("Enter player " + (i+1) + " information:");
System.out.print("Name:");
String name = scanner.next();
System.out.print("Height (feet):");
int feet = scanner.nextInt();
System.out.print("Height (inches):");
int inches = scanner.nextInt();
System.out.print("age:");
int age = scanner.nextInt();
players.add(new Player(name, new Height(feet, inches), age));
}
double totalAge = 0;
for (Player player : players) {
totalAge += player.getAge();
}
double averageAge = totalAge / players.size();
System.out.println("Average age: " + averageAge);
Player tallestPlayer = players.get(0);
for (int i = 1; i < players.size(); i++) {
Player player = players.get(i);
int totalFeet = player.getHeight().getFeets();
int totalInches = player.getHeight().toInches();
if (totalFeet > tallestPlayer.getHeight().getFeets() || (totalFeet == tallestPlayer.getHeight().getFeets() && totalInches > tallestPlayer.getHeight().toInches())) {
tallestPlayer = player;
}
}
}
System.out.println(“tallest player: ” + tallestPlayer.getName());
}
}
class Height {
private int feet;
private int inches;
public Height(int feet, int inches) {
this.feet = feet;
this.inches = inches;
}
public int toInches() {
return feet * 12 + inches;
}
public int getFeets() {
return feet;
}
@Override
public String toString() {
return feet + “‘:’” + inches;
}
}
class Player {
private String name;
private Height height;
private int age;
public Player(String name, Height height, int age) {
this.name = name;
this.height = height;
this.age = age;
}
public String getName() {
return name;
}
public Height getHeight() {
return height;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return “Name: ” + name + “, Hectaight: ” + height.toString() + “, Age: ” + age;
}
}
Leave a Reply