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