根据用户输入将人员平均分配到组

distrubuting people to groups evenly based on userinput

ReadFile randomGenerate = new ReadFile();
        Input userNum = new Input();
        String classnum = userNum.fileToSelect();
        int lineNumber = randomGenerate.lineCounter(classnum);

        //number of students in classnum.txt

        ArrayList<String> people = randomGenerate.readPeople();
        //a string of names(first word on each row)

        int userPut = userNum.numInput();
        int maxNum = lineNumber/userPut;

不对,我正在读取一个 txt 文件 - 每行的第一个单词是一个人的名字。我想将这个 ArrayList 的人分成偶数组,组数基于用户输入。

int[] numbers = RandomNumbersWithoutRepetition(0, lineNumber, lineNumber);       
//an array of numbers IN ORDER

int[] randomNumbers = RandomizeArray(numbers);
//shuffles the array

我按顺序生成了一组数字,然后将它们打乱,效果很好。

但是,我对人员进行分组的方法是基于 ifelse 条件,但效果不佳。那么有没有一种有效的方法可以将这个ArrayList的名称放入用户想要的组中?

我有 x 个学生。

我有 n 个组。

我假设没有其他标准可以将学生分组。

基本上,你把小组的数量分成学生的数量。是的,您必须处理余数。但这很简单。

这是测试的结果。我创建了 53 个学生并洗牌了他们。然后我把学生分成4组。我手动格式化输出以适应答案。第一组14人,其余组13人

Students: 42, 27, 5, 26, 32, 30, 44, 10, 17, 29, 40, 52, 
    47, 38, 49, 18, 46, 24, 34, 12, 13, 53, 35, 20, 1, 
    2, 41, 23, 43, 28, 8, 11, 50, 37, 9, 7, 48, 3, 33, 
    25, 31, 15, 22, 21, 14, 45, 36, 16, 51, 19, 4, 6, 39
Group 1: 42, 27, 5, 26, 32, 30, 44, 10, 17, 29, 40, 52, 47, 38
Group 2: 49, 18, 46, 24, 34, 12, 13, 53, 35, 20, 1, 2, 41
Group 3: 23, 43, 28, 8, 11, 50, 37, 9, 7, 48, 3, 33, 25
Group 4: 31, 15, 22, 21, 14, 45, 36, 16, 51, 19, 4, 6, 39

这是代码。 groupStudentssortStudents 方法是完成这项工作的方法。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class StudentGroups {

    public static void main(String[] args) {
        int numberOfGroups = 4;
        int numberOfStudents = 53;
        StudentGroups sg = new StudentGroups(numberOfStudents);
        sg.printStudents();
        List<List<Integer>> groups = sg.groupStudents(
                numberOfGroups);
        sg.printGroups(groups);
    }

    private List<Integer> students;

    public StudentGroups(int numberOfStudents) {
        students = new ArrayList<>(numberOfStudents);
        for (int i = 0; i < numberOfStudents; i++) {
            students.add(i + 1);
        }
        Collections.shuffle(students);
    }

    public void printStudents() {
        System.out.print("Students: ");
        for (int i = 0; i < students.size(); i++) {
            System.out.print(students.get(i));
            if (i < (students.size() - 1)) {
                System.out.print(", ");
            }
        }
        System.out.println();
    }

    public List<List<Integer>> groupStudents(int groups) {
        List<List<Integer>> output = new ArrayList<>();

        int size = students.size();
        int group = size / groups;
        int remainder = size % groups;
        sortStudents(output, size, group, remainder);

        return output;
    }

    private void sortStudents(List<List<Integer>> output, 
            int size, int group, int remainder) {
        List<Integer> list = new ArrayList<>();
        int count = 0;

        for (int i = 0; i < size; i++) {            
            list.add(students.get(i));

            if (count == 0 && remainder > 0) {
                if (group > 0) {
                    list.add(students.get(++i));
                }
                remainder--;
            }

            if (++count >= group) {
                addList(output, list);
                list = new ArrayList<>();
                count = 0;
            }
        }

        addList(output, list);
    }

    private void addList(List<List<Integer>> output, 
            List<Integer> list) {
        if (list.size() > 0) {
            output.add(list);
        }
    }

    public void printGroups(List<List<Integer>> groups) {
        for (int j = 0; j < groups.size(); j++) {
            System.out.print("Group ");
            System.out.print(j + 1);
            System.out.print(": ");
            List<Integer> students = groups.get(j);
            for (int i = 0; i < students.size(); i++) {
                System.out.print(students.get(i));
                if (i < (students.size() - 1)) {
                    System.out.print(", ");
                }
            }
            System.out.println();
        }
    }

}