lundi 27 mai 2019

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. If 0 is entered, nothing happens and the application continues on prompting the user to enter 1 or 2. See Fig-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. See also Fig-3, where 1 and 3 are entered for making one team and three teams, respectively. Hint: Use the Random class as seen in GetRandom1.java or GR.zip of Quiz 3 to implement this random selection.
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. 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. See Fig-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. See Fig-5. All leading and trailing spaces in the input would be ignored. Hint: use 'trim()' method of String. The application display two lines of dashes, i.e., "-------------------" 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)". 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(), substring(), and equals() methods of String to implement the above word count task. 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. 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. 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 = "";`




Aucun commentaire:

Enregistrer un commentaire