简体   繁体   English

如何使用 else-if 语句在 java 中编写随机组生成器?

[英]How to write a random group generator in java using else-if statements?

I have to code a program that allows the user to choose between two task: a random group generator, and a task that parses and counts an input sentence by word.我必须编写一个程序,允许用户在两个任务之间进行选择:一个随机组生成器,以及一个逐字解析和计算输入句子的任务。 I am really confused on how to go about it.我真的很困惑如何去做。 The instructions are: Team Maker: If 1 is entered, the application prompts the user to enter the desired number of teams to make.说明如下: Team Maker:如果输入 1,应用程序会提示用户输入所需的团队数量。 If 0 is entered, nothing happens and the application continues on prompting the user to enter 1 or 2. See Fig-3.如果输入 0,则什么也不会发生,应用程序会继续提示用户输入 1 或 2。请参见图 3。 If the number of teams is 1 or greater, the application displays the team number, beginning from 1, followed by three full names randomly selected from the 49 students provided in COP2510.txt.如果团队数量为 1 或更多,应用程序将显示团队编号,从 1 开始,后跟从 COP2510.txt 中提供的 49 名学生中随机选择的三个全名。 See also Fig-3, where 1 and 3 are entered for making one team and three teams, respectively.另请参见图 3,其中输入 1 和 3 分别表示组成一个团队和三个团队。 Hint: Use the Random class as seen in GetRandom1.java or GR.zip of Quiz 3 to implement this random selection.提示:使用 Random 类,如 GetRandom1.java 或测验 3 的 GR.zip 中所示,以实现此随机选择。
All names of each team must be displayed beginning with a 'tab'.每个团队的所有名称都必须以“制表符”开头显示。 It's very important in this application that no student appears in the same or different teams more than once.在此应用程序中非常重要的是,没有学生多次出现在相同或不同的团队中。 Hint: there are more than one way to "map" a random number (integer) to a specific student name.提示:有不止一种方法可以将随机数(整数)“映射”到特定学生姓名。 Using if....else if....else if....else if.... is one possible approach and is recommended here.使用 if....else if....else if....else if.... 是一种可能的方法,建议在此处使用。 Storing all names in an array is another way but is not introduced until Chapter 10. Counting Words: If 2 is entered, the application prompts the user to enter one or more sentences.将所有名称存储在数组中是另一种方法,但直到第 10 章才介绍。 计算单词:如果输入 2,应用程序会提示用户输入一个或多个句子。 See Fig-4.见图 4。 The application uses the space character, " ", to separate and count words in the input.该应用程序使用空格字符“”来分隔和计算输入中的单词。 If there are two or more consecutive spaces, they are treated as just one.如果有两个或多个连续空格,则将它们视为一个。 That is, "AA BB" and "AA BB" both contains two words.也就是说,“AA BB”和“AA BB”都包含两个词。 See Fig-5.见图 5。 All leading and trailing spaces in the input would be ignored.输入中的所有前导和尾随空格都将被忽略。 Hint: use 'trim()' method of String.提示:使用 String 的 'trim()' 方法。 The application display two lines of dashes, ie, "-------------------" to enclose every word it finds.该应用程序显示两行破折号,即“--------------------”以包含它找到的每个单词。 Each word must be displayed together with its length.每个单词必须与其长度一起显示。 For example, if "Hi, John," is entered, the two lines between dashes should be "Hi. (3)" and "John,(5)".例如,如果输入“Hi, John,”,则破折号之间的两行应为“Hi. (3)”和“John,(5)”。 After the 2nd dashes line, the total number of words is displayed to end the task.在第二条虚线之后,显示单词总数以结束任务。 If no words or sentences are entered.如果没有输入单词或句子。 a message of "Nothing or only space(s) is entered." “未输入任何内容或仅输入空格”的消息。 is displayed between the two dashes lines and the count is zero: See the last input in Fig-5, Hint, You may use trim(), indexOf(), length().显示在两条虚线之间并且计数为零:请参见图 5 中的最后一个输入,提示,您可以使用 trim()、indexOf()、length()。 substring(), and equals() methods of String to implement the above word count task. String 的 substring() 和 equals() 方法来实现上述字数统计任务。 Even the same methods are used, there are different approaches to get this task completed.即使使用相同的方法,也有不同的方法来完成这项任务。

I got the first part where the program welcomes the user, and shows what the program does.我得到了程序欢迎用户的第一部分,并展示了程序的作用。 However I don't know how to code the random team generator.但是我不知道如何编写随机团队生成器的代码。 I can only use else if statements.我只能使用 else if 语句。 Someone told me to assign a random number to each name and then use the else if statements, however I have no idea how to do that.有人告诉我为每个名字分配一个随机数,然后使用 else if 语句,但我不知道该怎么做。 And as for the word counter I just have no clue.至于计数器这个词,我只是不知道。 If anyone could help that would be great.如果有人可以提供帮助,那就太好了。

   import java.util.Scanner;
   import java.util.Random;
   public class asssignment3 {
   public static void main (String args[]){
   Scanner sc = new Scanner(System.in);
    //print out a task prompt 
    System.out.println("Assignment-3 can perform the following two task:");
    System.out.println();
    System.out.println("\t1-Making 3-member tems from class of COP2510");
    System.out.println("\t2-Parsing and counting an input sentence by word");
    System.out.println();
    System.out.print("Enter 1 or 2 to begin your task or anything else to quit:   ");
    String choiceNumber = sc.nextLine (); 

  if (choiceNumber.equalsIgnoreCase("0")) {
      System.out.println("Enter 1 or 2 to begin your task or anything else to quit:  ");
      String optionNumber = sc.nextLine ();
  }
  Random r = new Random();    
 if (choiceNumber.equalsIgnoreCase ("1")) {
     String studentName = "";`

Personally, I would forego the if-else and do the array.就个人而言,我会放弃 if-else 并做数组。 Make an array with every integer from 0-N, where N is the number of the students.用 0-N 中的每个整数创建一个数组,其中 N 是学生的数量。 Then, shuffle the list around.然后,将列表洗牌。 I believe Java has a prebuilt function for it, but if not the algorithm for one is trivial.我相信 Java 有一个预建的功能,但如果没有,一个算法是微不足道的。

From there, simply read in the student file line by line and assign the corresponding number from the shuffled array.从那里,只需逐行读入学生文件并从打乱的数组中分配相应的数字。 Example psuedocode:示例伪代码:


# >>> Generate the list of numbers of shuffle it. <<< #
N = number of students
numbers = [i for i in range(N)]
random.shuffle(numbers)

# >>> Initialize a list to store number-student pair <<< #
students = []

# >>> Read in the student file line by line <<< #
for line in students.txt:
    randomNumber = numbers.pop()
    students.append((line, randomNumber))

Viola, the students list now has all of the students, paired with a random number.中提琴, students列表现在包含所有学生,并与一个随机数配对。

As for counting words, run the input through trim() and then split().至于计算单词,通过 trim() 运行输入,然后运行 split()。

# >>> Removes whitespace before and after the string ("  A " -> "A") <<< #
input = input.trim()
# >>> Splits input into an array ("AA BB CC" -> ["AA", "BB", "CC"]) <<< #
words = input.split(" ")
numWords = word.length

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM