Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *This code makes random groups of classmates.
- *
- * @Molly Limaye
- * @09/24/19
- */
- import java.util.*;
- public class RandomGroups {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int run = 1;
- while (run==1){
- //making class array
- String[] csclass = {"Arjun", "Robert", "Petey", "Maya", "Nikhil", "Shane","Molly", "Neera", "Jackson", "Chris", "Michael"};
- //user input
- System.out.println("How many people should be in each group?");
- int groupsize = sc.nextInt();
- //variables
- int groups = (int) (csclass.length/groupsize);
- int remainder = csclass.length%groupsize;
- int groupnum = 1;
- int index=0;
- String member = "";
- //choosing group members
- for( int i = 0; i <groups;i++){
- System.out.print("\nGroup " + groupnum + ": ");
- //remainder check
- if(remainder>0){
- index = randomNum(0,10);
- while(csclass[index].equals("null")){
- index = randomNum(0,10);
- }
- member = csclass[index];
- csclass[index]="null";
- System.out.print(member + " ");
- remainder --;
- }
- //assigning groups
- for(int a = 0; a < groupsize;a++){
- index = randomNum(0,10);
- while(csclass[index].equals("null")){
- index = randomNum(0,10);
- }
- member = csclass[index];
- csclass[index]="null";
- System.out.print(member + " ");
- }
- groupnum++;
- }
- //play again feature
- System.out.println("\nWould you like to assign new groups? (yes/no)");
- String answer = sc.next();
- if (answer.equalsIgnoreCase("yes")){
- run = 1;
- }
- else{
- run = 0;
- System.out.println("End");
- }
- }
- }
- public static int randomNum(int min, int max) {
- int randomNum = (int) (Math.random() * ((max-min) + 1) + min);
- return randomNum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement