Grouping program v1.0
Loader class
Main class
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
private Scanner sc;
int groups;
ArrayList<String> students;
public Main() {
main();
}
public void main() {
this.printTitle();
print("Press Enter to Continue...");
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
print("How many groups?");
groups = Integer.parseInt(input());
print("Enter the names of the students");
print("Enter 0 when done");
students = new ArrayList<String>();
while (true) {
String name = input();
if (name.equals("0")) {
break;
}
students.add(name);
System.out.println(students);
}
this.grouping();
}
private void grouping() {
if (groups > students.size()) {
print("group too big");
} else if (groups == students.size()) {
print("are you kidding me?");
} else {
System.out.println("names: " + students);
Collections.shuffle(students);
System.out.println("shuffled names: " + students);
int perGroup = students.size() / groups;
ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
for (int i = 0; i < groups; i++) {
ArrayList<String> names = new ArrayList<String>();
for (int j = 0; j < perGroup; j++) {
names.add(students.get(0));
students.remove(students.get(0));
}
result.add(names);
System.out.println("final result: " + result);
}
}
}
private void printTitle() {
System.out.println("****************************\r\n" + "\r\n" + " Grouping v1.0\r\n" + "\r\n"
+ " developed by Byul\r\n" + "****************************");
}
private void print(String s) {
System.out.println(s);
}
private String input() {
sc = new Scanner(System.in);
return sc.next();
}
}
댓글
댓글 쓰기