我的方法中有一个棒球运动员列表populateBaseballPlayers
,但是不是以方法中的数字形式显示棒球运动员的数量displayFavoriteBaseballPlayersData
,我怎样才能显示计数asterisks
*
?输出应该是Mike Trout : ***
Mookie Betts : *****
等等。
我知道这可能超级简单,但我迷失了。
这是我的完整代码:
FavoriteBaseballPlayers
类:
import java.util.ArrayList;
public class FavoriteBaseballPlayers
{
private ArrayList<String> favoriteBaseballPlayers;
/**
* Constructor to populate the list of baseball favorite baseball players
*/
public FavoriteBaseballPlayers()
{
favoriteBaseballPlayers = new ArrayList<>();
populateBaseballPlayers();
}
/**
* Pre-set the list of favorite baseball players
*/
private void populateBaseballPlayers()
{
favoriteBaseballPlayers.add("Bryce Harper");
favoriteBaseballPlayers.add("Aaron Judge");
favoriteBaseballPlayers.add("Clayton Kershaw");
favoriteBaseballPlayers.add("Mike Trout");
favoriteBaseballPlayers.add("Mike Trout");
favoriteBaseballPlayers.add("Bryce Harper");
favoriteBaseballPlayers.add("Mookie Betts");
favoriteBaseballPlayers.add("Francisco Lindor");
favoriteBaseballPlayers.add("Mookie Betts");
favoriteBaseballPlayers.add("Mookie Betts");
favoriteBaseballPlayers.add("Clayton Kershaw");
favoriteBaseballPlayers.add("Gerrit Cole");
favoriteBaseballPlayers.add("JD Martinez");
}
/**
* Get the list of favorite baseball players
* @return the baseball players in the ArrayList
*/
public ArrayList<String> getFavoriteBaseballPlayers()
{
return favoriteBaseballPlayers;
}
}
这是FavoriteBaseballPlayersDisplay
类:
import java.util.ArrayList;
public class FavoriteBaseballPlayersDisplay
{
private ArrayList<String> baseballPlayers;
private int[] countOfFavoriteBaseballPlayers;
/**
* Constructor for the objects fo the FavoriteBaseballPlayersDisplay class
*/
public FavoriteBaseballPlayersDisplay()
{
FavoriteBaseballPlayers players = new FavoriteBaseballPlayers();
baseballPlayers = players.getFavoriteBaseballPlayers();
scanBaseballPlayerData();
}
void printPlayerCount(final String player, final int count)
{
System.out.print(player + ":\t");
for (int i = 0; i < count; i++) {
System.out.print("*");
}
System.out.println();
}
/**
* Examine the baseball player data and document the counts of the baseball
* players
*/
private void scanBaseballPlayerData()
{
countOfFavoriteBaseballPlayers = new int[6];
for (String player : baseballPlayers) {
if (player.equals("Aaron Judge")) {
countOfFavoriteBaseballPlayers[0]++;
}
else if (player.equals("Bryce Harper")) {
countOfFavoriteBaseballPlayers[1]++;
}
else if (player.equals("Mookie Betts")) {
countOfFavoriteBaseballPlayers[2]++;
}
else if (player.equals("Mike Trout")) {
countOfFavoriteBaseballPlayers[3]++;
}
else if (player.equals("Clayton Kershaw")) {
countOfFavoriteBaseballPlayers[4]++;
}
else if (player.equals("Francisco Lindor")) {
countOfFavoriteBaseballPlayers[5]++;
}
else {
countOfFavoriteBaseballPlayers[6]++;
}
}
}
/**
* Display the baseball player data in the form of a histogram
*/
public void displayFavoriteBaseballPlayersData()
{
System.out.println("Here are the count of favorite baseball players that were selected as favorites:");
printPlayerCount("Aaron Judge: " + countOfFavoriteBaseballPlayers[0]);
printPlayerCount("Bryce Harper: " + countOfFavoriteBaseballPlayers[1]);
printPlayerCount("Mookie Betts: " + countOfFavoriteBaseballPlayers[2]);
printPlayerCount("Mike Trout: " + countOfFavoriteBaseballPlayers[3]);
printPlayerCount("Clayton Kershaw: " + countOfFavoriteBaseballPlayers[4]);
printPlayerCount("Francisco Lindor: " + countOfFavoriteBaseballPlayers[5]);
printPlayerCount("Other: " + countOfFavoriteBaseballPlayers[6]);
}
}
发布于 2019-03-12 18:25:23
无论如何,我建议你选择一个更简单的路径。而不是维护一个单独的array
(countOfFavoriteBaseballPlayers
),使用一个Stream
。您可以按玩家分组并收集到计数。
final Map<String, Long> count =
baseballPlayers.stream()
.collect(Collectors.groupingBy(
Function.identity(),
Collectors.counting()));
Function.identity()
等于lambda s -> s
,它基本上返回String
。
然后,您可能会有另一个方法接受播放器名称和出现次数
printPlayerCount("Bryce Harper", count.get("Bryce Harper"));
printPlayerCount("Aaron Judge", count.get("Aaron Judge"));
printPlayerCount("Clayton Kershaw", count.get("Clayton Kershaw"));
printPlayerCount("Mike Trout", count.get("Mike Trout"));
如
void printPlayerCount(final String player, final Long count) {
System.out.print(player + ":\t");
for (int i = 0; i < count; i++) {
System.out.print("*");
}
System.out.println();
}
这会打印出:
Bryce Harper: **
Aaron Judge: *
Clayton Kershaw: **
Mike Trout: **
要继续使用单独的array
,您还可以考虑提取方法
void printPlayerCount(final String player, final int count) {
System.out.print(player + ":\t");
for (int i = 0; i < count; i++) {
System.out.print("*");
}
System.out.println();
}
并使用:
printPlayerCount("Aaron Judge", countOfFavoriteBaseballPlayers[0]);
https://stackoverflow.com/questions/-100006437
复制相似问题