我有一个随机数量的用户列表和一个班级团队。 每个团队最多可容纳9名用户。 所以,我想把他们分成小组,但我被困住了。 如果用户是< 9但是, 如果用户是> 9意味着我必须创建一个新团队,然后在循环用户的同时将其他用户设置在新团队中。 我被卡住了,因为Team类的构造函数必须有2个用户。
主要
public class User {
private String _name;
private int _objId;
private Team _team;
public User(String name,int objId)
{
_name = name;
_objId = objId;
}
public String getName()
{
return _name;
}
public int getObjId()
{
return _objId;
}
public void setTeam(Team team)
{
_team = team;
}
用户
public class Team {
private final List<User> _users = new ArrayList<>();
public Team(User first, User second) {
_users.add(first);
_users.add(second);
}
public void add(User mem) {
_users.add(mem);
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("======== Team =========\n");
_users.forEach(user -> sb.append(user.getName() + "\n"));
sb.append("=======================\n");
return sb.toString();
}
public List<User> getUsers ()
{
return _users;
}
}
队
======== Team =========
Mohammad
John
Julieta
Kecia
Anna
Tanika
Alla
Lily
Pennie
=======================
}
在具体示例中,用户为9,输出为
Reminers.Activity.java
答案 0 :(得分:0)
通过一个简单的循环,我认为你可以解决它
int countDown=0;
for(User user : _users) {
if(countDown>=9){
countDown=0;
}
if(countDown==0){
//New team stuff
}
//Add the user to the new team.
//...
//Update count down
countDown++;
}
<强> EDITED 强>
只需补充您的列表大小的玩家数量。然后你将不得不创建N个团队,每个团队numberOfPlayerEachTeam
。
public static void main(String[] args)
{
int numberOfPlayers = 10;
int maxNumberOfPlayerForTeam = 9;
int minNumberOfPlayersForTeam = 2;
int numberOfPlayerEachTeam = 0;
for(int i =maxNumberOfPlayerForTeam; i >= minNumberOfPlayersForTeam && numberOfPlayerEachTeam <= 0 ; i--){
int module = numberOfPlayers % i;
if( module >= minNumberOfPlayersForTeam && module <=maxNumberOfPlayerForTeam ){
numberOfPlayerEachTeam = i;
}
}
System.out.println("Number of players for each team: " + numberOfPlayerEachTeam);
}
答案 1 :(得分:0)
我建议你对你的业务逻辑做一些重构&#34;一点点。
我认为您不需要Team
课程,这会让您的生活更轻松。
您的User
课程看起来不错,但我建议您覆盖toString
方法,以帮助您显示结果:
public class User {
private String name;
private Integer id;
// constructor
public User(final String name, final Integer id) {
this.name = name;
this.id = id;
}
@Override
public String toString() {
return name + ":" + id;
}
// getter and setter methods
}
这是在团队之间分割用户的主要应用程序。这里最重要的方法是calculateTeams
,它们在团队之间分割用户:
public class AllocateTeams {
private static final int MEMBERS_IN_TEAM = 9;
public static void main(String[] args) {
List<User> users = getUsers();
//int numberOfTeams = getNumberOfTeams(users);
List<List<User>> teams = calculateTeams(users, MEMBERS_IN_TEAM);
System.out.println(String.format("Number of the individual players: %d", users.size()));
System.out.println(String.format("Maximum players in a team: %d", MEMBERS_IN_TEAM));
System.out.println(String.format("Number of the teams: %d", teams.size()));
printTeams(teams);
}
private static void printTeams(final List<List<User>> teams) {
int teamIndex = 0;
for (List<User> team : teams) {
teamIndex++;
System.out.println(String.format("---------- team %d ----------", teamIndex));
System.out.println(team);
}
}
private static List<List<User>> calculateTeams(final List<User> users, final int memberInTeam) {
List<List<User>> teams = new ArrayList<>();
int teamNumber = getNumberOfTeams(users, memberInTeam);
for (int teamIndex = 0; teamIndex < teamNumber; teamIndex++) {
List<User> team = new ArrayList<>();
for (int userIndexPerTeam = 0; userIndexPerTeam < memberInTeam; userIndexPerTeam++) {
int userIndex = userIndexPerTeam + (teamIndex * memberInTeam);
if (userIndex < users.size()) {
team.add(users.get(userIndex));
}
}
teams.add(team);
}
return teams;
}
private static int getNumberOfTeams(final List<User> users, final int memberInTeam) {
return (users.size() + memberInTeam - 1) / memberInTeam;
}
private static List<User> getUsers() {
int id = 0;
List<User> users = new ArrayList<>();
users.add(new User("Peter", id++));
users.add(new User("Alfonso", id++));
users.add(new User("Arthur", id++));
users.add(new User("Ashley", id++));
users.add(new User("Arnold", id++));
users.add(new User("Andy", id++));
users.add(new User("Augustus", id++));
users.add(new User("Angelo", id++));
users.add(new User("Alan", id++));
users.add(new User("Albert", id++));
users.add(new User("Nick", id++));
users.add(new User("Nicolas", id++));
users.add(new User("Norris", id++));
users.add(new User("Fiona", id++));
users.add(new User("Fidela", id++));
users.add(new User("Fanny", id++));
users.add(new User("Francesca", id++));
users.add(new User("Paula", id++));
users.add(new User("Eliza", id++));
users.add(new User("Elizbeth", id++));
users.add(new User("Ernestina", id++));
users.add(new User("Sharda", id++));
users.add(new User("Shara", id++));
Collections.shuffle(users);
return users;
}
}
结果如下:
Number of the individual players: 23
Maximum players in a team: 9
Number of the teams: 3
---------- team 1 ----------
[Albert:9, Norris:12, Francesca:16, Paula:17, Ernestina:20, Alan:8, Fidela:14, Nicolas:11, Fanny:15]
---------- team 2 ----------
[Andy:5, Fiona:13, Peter:0, Shara:22, Arnold:4, Elizbeth:19, Eliza:18, Sharda:21, Augustus:6]
---------- team 3 ----------
[Arthur:2, Nick:10, Ashley:3, Angelo:7, Alfonso:1]
我希望你能理解的代码是明确的。