Question: How would I efficiently be able to use only one person and then when the code runs again it would not assign that same student to a group. Basically not use a student twice. I attempted to fix this problem however it ended me in a stuck dead end. Any help is appreciated, thanks!
Create an Array of the students in this class (First and Last Name).
Then randomly choose a student and place them in groups.
Group 1 - 4 Students
Group 2 - 4 Students
Group 3 - 3 Students
Group 4 - 4 Students
Group 5 - 4 Students
Group 6 - 4 Students
Then Print out the groups in an organized output. Code:
import java.util.*;
public class RandomGroupsTest {
static String[] group = {"Group 1","Group 2","Group 3", "Group 4","Group 5", "Group 6"};
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] students= {"Krish", "Mami Bhat", "Kilar Boggavarapu", "Nickel Gokul", "Matthew Guan", "Maysin Huang", "Kashan Karvadi", "Kilan Manivannan", "Dine Marin", "Dhruv Patel", "Mans Patel", "Vin Pham", "Sonyiea Rayman", "Mahi Sangavarapu", "Reyun Shah", "Prevan Shanmugaraj", "Riya Singh", "Palk Singh", "Rohan Singh", "Callvin Stapleton", "Arjun Vyas", "John Williams", "Alisha White" };
Random rn = new Random();
int a =0;
int i = 0;
group[i] = "Group 1";
System.out.print("Group 1: ");
if(group[i].equals("Group 1")) {
for(int d = 0; d < 4; d++) {
a = rn.nextInt(23);
if(students[a]!="") {
System.out.print("\n "+students[a]);
students[a]= "";
}else {
a=rn.nextInt(23);
if(students[a]!="") {
System.out.println("\n "+students[a]);
}
}
}
i++;
System.out.println();
}
System.out.print("Group 2: ");
if(group[i].equals("Group 2")) {
for(int w = 0; w < 4; w++) {
a = rn.nextInt(23);
if(students[a]!="") {
System.out.print("\n "+students[a]);
students[a]= "";
}else {
a=rn.nextInt(23);
if(students[a]!="") {
System.out.println("\n "+students[a]);
}
}
}
i++;
System.out.println();
}
System.out.print("Group 3: ");
if(group[i].equals("Group 3")) {
for(int v = 0; v < 3; v++) {
a = rn.nextInt(23);
if(students[a]!="") {
System.out.print("\n "+students[a]);
students[a]= "";
}else {
a=rn.nextInt(23);
if(students[a]!="") {
System.out.println("\n "+students[a]);
}
}
}
i++;
System.out.println();
}
System.out.print("Group 4: ");
if(group[i].equals("Group 4")) {
for(int b = 0; b < 4; b++) {
a = rn.nextInt(23);
if(students[a]!="") {
System.out.print("\n "+students[a]);
students[a]= "";
}else {
a=rn.nextInt(23);
if(students[a]!="") {
System.out.println("\n "+students[a]);
}
}
}
i++;
System.out.println();
}
System.out.print("Group 5: ");
if(group[i].equals("Group 5")) {
for(int w = 0; w < 4; w++) {
a = rn.nextInt(23);
if(students[a]!="") {
System.out.print("\n "+students[a]);
students[a]= "";
}else {
a=rn.nextInt(23);
if(students[a]!="") {
System.out.println("\n "+students[a]);
}
}
}
i++;
System.out.println();
}
System.out.print("Group 6: ");
if(group[i].equals("Group 6")) {
for(int n = 0; n< 4; n++) {
a = rn.nextInt(23);
if(students[a]!="") {
System.out.print("\n "+students[a]);
students[a]= "";
}else {
a=rn.nextInt(23);
if(students[a]!="") {
System.out.println("\n "+students[a]);
}
}
}
i++;
System.out.println();
}
}
}
It can easily be solved using one-dimensional arrays and one-level deep nested loops. The solution given below is fairly simple to understand:
A sample run:
Feel free to comment in case of any doubt/issue.