Reputation: 11
Hey, I'm new to programming and was wondering how I could write this code using if statements and modules. I know how to do the first part but am stuck on how to distribute the remainder of the students into the rest of the groups. My program so far is down below:
package grouping.problem;
import java.util.Scanner;
public class GroupingProblem {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of students: ");
int students = sc.nextInt();
System.out.println("Enter the number of groups: ");
int groups = sc.nextInt();
int groupsmade;
int randomgroups;
int studentin = 0;
int remainder;
if (students<=0 || groups<=0){
System.out.println("Invalid Input");
System.exit(0);
}
if (students%groups == 0){
studentin = students/groups;
groupsmade = students/studentin;
System.out.println("There are " + groupsmade + " groups of " + studentin + " students each");
}else if (students % groups != 0){
remainder = (students % groups);
System.out.println("Not an even amount of students");
}
} }
Upvotes: 1
Views: 103
Reputation: 22471
The value of students % groups
is how many groups there will be with an additional student:
import java.util.Scanner;
public class GroupingProblem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Input number of students: ");
int students = scanner.nextInt();
System.out.print("Input number of groups: ");
int groups = scanner.nextInt();
if (students <= 0 || groups <= 0) {
System.out.println("Invalid Input");
System.exit(1);
}
int studentsLeftAfterEvenDistribution = students % groups;
int baseGroupSize = students / groups;
int groupsWithBaseGroupSize = groups - studentsLeftAfterEvenDistribution;
System.out.printf("There %s %d group%s of %d student%s each.%n",
areOrIs(groupsWithBaseGroupSize), groupsWithBaseGroupSize,
possiblyAddStoPluralize(groupsWithBaseGroupSize), baseGroupSize,
possiblyAddStoPluralize(baseGroupSize));
if (studentsLeftAfterEvenDistribution != 0) {
System.out.printf("There %s %d group%s of %d student%s each.%n",
areOrIs(studentsLeftAfterEvenDistribution), studentsLeftAfterEvenDistribution,
possiblyAddStoPluralize(studentsLeftAfterEvenDistribution), baseGroupSize + 1,
possiblyAddStoPluralize(baseGroupSize + 1));
}
}
private static String areOrIs(int groups) {
return groups != 1 ? "are" : "is";
}
private static String possiblyAddStoPluralize(int num) {
return num != 1 ? "s" : "";
}
}
Example Usage 1:
Input number of students: 20
Input number of groups: 4
There are 4 groups of 5 students each.
Example Usage 2:
Input number of students: 22
Input number of groups: 5
There are 3 groups of 4 students each.
There are 2 groups of 5 students each.
Example Usage 3:
Input number of students: 3
Input number of groups: 5
There are 2 groups of 0 students each.
There are 3 groups of 1 student each.
Example Usage 4:
Input number of students: 0
Input number of groups: 10
Invalid Input
Example Usage 5:
Input number of students: 5
Input number of groups: 4
There are 3 groups of 1 student each.
There is 1 group of 2 students each.
Upvotes: 1