3 回答
TA贡献1757条经验 获得超7个赞
以前的答案效果很好。这是一个简化代码和提高可读性的想法(虽然它做同样的事情!):
BaseBallTeam kate1 = new BaseBallTeam("Kate", 2.0);
BaseBallTeam kate2 = new BaseBallTeam("Kate", 3.0);
BaseBallTeam mike1 = new BaseBallTeam("Mike", 1.0);
BaseBallTeam mike2 = new BaseBallTeam("Mike", 5.0);
List<BaseBallTeam> list = Arrays.asList(kate1, kate2, mike1, mike2);
ArrayList<String> teamMembers = new ArrayList<>();
for(BaseBallTeam team : list){
if(!teamMembers.contains(team.getTeamMember())){
teamMembers.add(team.getTeamMember());
}
}
double totalRuns = 0;
for(String teamMember : teamMembers){
double runs = 0;
for(BaseBallTeam team : list){
if(team.getTeamMember().equals(teamMember)){
runs += team.getHomeRuns();
}
}
System.out.println("Amount of " + teamMember + "s hit " + runs + " home runs");
totalRuns += runs;
}
System.out.println("Total homeruns " + totalRuns);
添加回答
举报