老实说,我不知道该怎么做。说明在链接中。输入时,我必须制作三组随机名称。我的工作在链接中。此外,其中一些位于下方。这主要是错误的。
https://docs.google.com/document/d/1Pc77N19zc1XEXXqE223Lz4Wwaiwx0JUcxUt01vdAkoU/edit
随机组生成器
说明: 该应用程序将询问需要多少组。用户输入以粗体显示。 输入组数:3 输入有效整数后,应用程序将要求用户输入学生姓名。 输入学生姓名:Trudy Weigel 假设在用户按下Enter键之前键入的任何内容均为有效名称。继续输入名称,直到输入“ x”为止。
Here's a sample:
Enter number of groups: 3
Enter the student name (or x to exit): Fred
Enter the student name (or x to exit): Barney
Enter the student name (or x to exit): Wilma
Enter the student name (or x to exit): Betty
Enter the student name (or x to exit): Bam Bam
Enter the student name (or x to exit): Pebbles
Enter the student name (or x to exit): Dino
Enter the student name (or x to exit): x
The “x” concludes the list of student names.
Once the user has entered all of the names, the application will process the information and output the groups. Students should be randomly assigned to groups. (The shuffle method is very useful here!) The number of students per group should be maximized; however, it is possible that some groups will have one more student in the group than other groups. No student can be in more than one group. No student should be left out; every student should be in one group.
For example, I may ask for 3 groups from 11 students (that are entered from the console).
The student’s names may be:
Trudy Weigel
Jim Dangle
Jonesy
Raineesha Williams
Clementine Johnson
Junior
James Garcia
C. Kimball
Frank Rizzo
Jack Declan
Mayor Hernandez
Your output must be formatted as below. The 3 groups might be:
Group #1
Jack Declan
Jim Dangle
Mayor Hernandez
Raineesha Williams
Group #2
C. Kimball
Trudy Weigel
Clementine Johnson
Junior
Group #3
Jonesy
Frank Rizzo
James Garcia
您必须使用Java类ArrayList和Random来实现。实际上,您必须使用ArrayLists的ArrayList。同样,所有逻辑都可能未包含在主程序中。您必须在本练习中找到一种重用代码的方法。
Therefore, the definition of another class is required with:
1. a constructor
2. at least one instance variable
3. at least one method that returns a value
命名包含主要方法RandomGroupGenerator.java的程序
这是我的工作。
public class RandomGroupGenerator {
// variables for three groups
private String first;
private String second;
private String third;
// constructor for the three groups
public RandomGroupGenerator(String first, String second, String third) {
super();
this.first = first;
this.second = second;
this.third = third;
}
// getter and setters for the three groups
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getSecond() {
return second;
}
public void setSecond(String second) {
this.second = second;
}
public String getThird() {
return third;
}
public void setThird(String third) {
this.third = third;
}
// to string method to get the variables in group
@Override
public String toString() {
return "RandomGroupGenerator [first=" + first + ", second=" + second + ", third=" + third + "]";
}
}
public class RandomGroupGeneratorTester {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// ArrayList<String> names = new ArrayList<String>();
System.out.println("Enter the student name (or x to exit)");
// while loop to exit when x is entered or keep typing names
while (!input.hasNext("x")) {
System.out.println("Enter the student name (or x to exit)");
RandomGroupGenerator gen = new RandomGroupGenerator(input.next(), input.next(), input.next());
System.out.println(gen);
}
}
}
该代码应该键入随机名称,然后将它们分为三个不同的组。我没有收到错误,但这是错误的。我需要获得正确答案的帮助。