Пожалуйста, помогите мне решить проблему java
Моя третья поисковая команда выбора, когда я ищу команду 1 Барона, но не могу отобразить имя игрока и счет. Последний выбор показать список лидеров и не аккуратно. Пожалуйста, дайте мне какую-нибудь идею, чтобы решить эту проблему
Что я уже пробовал:
import java.util.Random; import java.util.Scanner; import static array.Main.makeSelection; import static array.Main.numberInputValidation; import static array.Main.numberOfTeams; public class Main { static Scanner scanner = new Scanner(System.in); static Team[] currentTeams = new Team[10]; public static void main(String[] args){ createTeams(); while (true) { int selection = menu(); switch (selection) { case 1: addTeam(); break; case 2: addPlayer(); break; case 3: searchTeam(); break; case 4: showLeaderboard(); break; case 5: System.out.println("\n Thank you for using "); System.exit(1); } } } public static int menu() { System.out.println(" "); System.out.println("1. Add Team "); System.out.println("2. Add player to a team "); System.out.println("3. Search for a team "); System.out.println("4. Show Leaderboard "); System.out.println("5. Exit "); System.out.println("Please select an option : "); return makeSelection(5); } public static void createTeams() { String[] teamName = {"Baron", "Eagle", "Ace", "Mango", "Juicy" }; String[] countryname = {"Malaysia", "Singapore", "Indonesia", "Myanmar", "Philippine" }; String[] playerNames = {"Ali","Abu","Amad","Chan","Tan","Puri","Aiku","Dina","Kimberly","Niger","Adam","Kok"}; for (int i = 0; i < teamName.length; i++) { currentTeams[i] = new Team(teamName[i], countryname[i]); int randomTeamMemberSize = new Random().nextInt(5 + 1) + 3; for (int j = 0; j < 2 ; j++){ currentTeams[i].addPlayer(playerNames[new Random().nextInt(11 + 1)], new Random().nextInt(100 + 10)); } } } public static int makeSelection(int numberOfOptions) { int selection = 0; while (true) { try { String select = scanner.nextLine(); selection = Integer.parseInt(select); if (selection <= 0 || selection > numberOfOptions) { throw new NumberFormatException(); } else { return selection; } } catch (NumberFormatException e) { System.out.println("Invalid option, please enter again : "); } } } public static char makeSelection() { System.out.println("Do you wish to continue? Y/N"); String selection = scanner.nextLine(); while (selection.length() > 1 || (selection.charAt(0) != 'Y' && selection.charAt(0) != 'N')) { System.out.println("Invalid option, please enter again: "); selection = scanner.nextLine(); } return selection.charAt(0); } public static int numberInputValidation() { int number = 0; while (true) { try { String num = scanner.nextLine(); number = Integer.parseInt(num); if (number < 0) { throw new NumberFormatException(); } return number; } catch (NumberFormatException e) { System.out.println("Please enter a valid number: "); } } } public static int numberOfTeams() { int numberOfTeams = 0; for (Team currentTeam : currentTeams) { if (currentTeam != null) { numberOfTeams++; } else { break; } } return numberOfTeams; } public static void addTeam() { String name, country; if (currentTeams[currentTeams.length - 1] == null) { System.out.println("Please enter a team name : "); name = scanner.nextLine(); System.out.println("Please enter the team's country:"); country = scanner.nextLine(); for (int i = 0; i < currentTeams.length; i++) { if (currentTeams[i] == null) { currentTeams[i] = new Team(name, country); break; } } System.out.println("Team added succesfully !"); } else { System.out.println("Sorry, there is currently no slots for a new team"); } System.out.println("Press enter to continue.."); scanner.nextLine(); } public static void addPlayer() { while (true) { int numberOfTeams = numberOfTeams(); System.out.println("No. Team "); for (int i = 0; i < numberOfTeams; i++) { System.out.printf("|%-2s.|%-42s|\n", Integer.toString(i + 1), currentTeams[i].getName()); } System.out.println("Please select a team : "); int selection = makeSelection(numberOfTeams) - 1; if (currentTeams[selection].TeamFull()) { System.out.println("Sorry, the team is already full."); } else { String name; int score; System.out.println("Please enter the player's name"); name = scanner.nextLine(); System.out.println("Please enter the player's score:"); score = numberInputValidation(); currentTeams[selection].addPlayer(name, score); System.out.println("" + name + " added to team " + currentTeams[selection].getName() + " successfully !"); } if (makeSelection() == 'N') { break; } } } public static void searchTeam() { while (true) { System.out.println("Please enter a team name : "); String name = scanner.nextLine(); Team result = search(name); result = search(name); if (result == null) { System.out.println("No results found !"); } else { System.out.println(); System.out.printf("Team %s from %s\n", result.getName(), result.getCountryname()); System.out.println("|No.| Player's Name | Score |"); Player[] players = result.getPlayer(); if (result.TeamEmpty()) { System.out.println("| Team is Empty! |"); } else { for (int i = 0; i < players.length; i++) { if (players[i] == null) { break; } System.out.printf("|%-2s.|%-28s|%-13s|\n", Integer.toString(i + 1), players[i].getName(), Integer.toString(players[i].getScore())); } } } if (makeSelection() == 'N') { break; } } } public static Team search(String name) { for (Team currentTeam : currentTeams) { if (currentTeam == null) { break; } if (currentTeam.getName().equals(name)) { return currentTeam; } } return null; } public static void showLeaderboard() { Team[] tmpTeam = new Team[numberOfTeams()]; System.arraycopy(currentTeams, 0, tmpTeam, 0, tmpTeam.length); int n = tmpTeam.length; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (tmpTeam[j].getScore() < tmpTeam[j + 1].getScore()) { // swap arr[j+1] and arr[j] Team temp = tmpTeam[j]; tmpTeam[j] = tmpTeam[j + 1]; tmpTeam[j + 1] = temp; } } } System.out.println(" Leaderboard "); System.out.println(" No. Team Country Score "); for (int i = 0; i < tmpTeam.length; i++) { System.out.printf("|%-2s.|%-28s|%-25s|%-13s|", Integer.toString(i + 1), tmpTeam[i].getName(),tmpTeam[i].getCountryname(), Integer.toString(tmpTeam[i].getScore())+"\n"); } System.out.println("Press enter to continue.."); scanner.nextLine(); } }