Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. /**
  2. *This code makes random groups of classmates.
  3. *
  4. * @Molly Limaye
  5. * @09/24/19
  6. */
  7.  
  8. import java.util.*;
  9.  
  10. public class RandomGroups {
  11.  
  12. public static void main(String[] args) {
  13.  
  14. Scanner sc = new Scanner(System.in);
  15.  
  16. int run = 1;
  17.  
  18. while (run==1){
  19. //making class array
  20. String[] csclass = {"Arjun", "Robert", "Petey", "Maya", "Nikhil", "Shane","Molly", "Neera", "Jackson", "Chris", "Michael"};
  21. //user input
  22. System.out.println("How many people should be in each group?");
  23. int groupsize = sc.nextInt();
  24.  
  25. //variables
  26. int groups = (int) (csclass.length/groupsize);
  27. int remainder = csclass.length%groupsize;
  28. int groupnum = 1;
  29. int index=0;
  30. String member = "";
  31.  
  32. //choosing group members
  33. for( int i = 0; i <groups;i++){
  34. System.out.print("\nGroup " + groupnum + ": ");
  35. //remainder check
  36. if(remainder>0){
  37. index = randomNum(0,10);
  38. while(csclass[index].equals("null")){
  39. index = randomNum(0,10);
  40. }
  41. member = csclass[index];
  42. csclass[index]="null";
  43. System.out.print(member + " ");
  44. remainder --;
  45. }
  46. //assigning groups
  47. for(int a = 0; a < groupsize;a++){
  48. index = randomNum(0,10);
  49. while(csclass[index].equals("null")){
  50. index = randomNum(0,10);
  51. }
  52. member = csclass[index];
  53. csclass[index]="null";
  54. System.out.print(member + " ");
  55. }
  56. groupnum++;
  57. }
  58.  
  59. //play again feature
  60. System.out.println("\nWould you like to assign new groups? (yes/no)");
  61. String answer = sc.next();
  62. if (answer.equalsIgnoreCase("yes")){
  63. run = 1;
  64. }
  65. else{
  66. run = 0;
  67. System.out.println("End");
  68. }
  69. }
  70. }
  71.  
  72. public static int randomNum(int min, int max) {
  73. int randomNum = (int) (Math.random() * ((max-min) + 1) + min);
  74. return randomNum;
  75.  
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement